Implement Custom Logger with slf4j

妖精的绣舞 提交于 2019-12-17 15:46:47

问题


I want to implement a Custom logger which logs all log entries to a Database. Currently my app logs this way (slf4j and log4j binding):

private static final Logger logger = LoggerFactory.getLogger( MyClass.class );

I'm not sure how to proceed. My Idea is to implement a Custom Logging binding through implementing the org.slf4j.Logger Interface

What would be the next steps? My target is to not change the current code

Links I considered:

  • Java custom logger: logging standards or/and best practices
  • http://www.slf4j.org/manual.html

回答1:


it should be fairly easy. you'll need to implement your own Logger and LoggerFactory. you will not have to change existing code at all.

after doing that you'll need to implement StaticLoggerBinder to return your logger factory and class name. if you download the slf4j zip file then you get the source for all the implementations too, just have a look at the StaticLoggerBinder in slf4j-log4j for an example.

have a look at this link for details : http://www.slf4j.org/faq.html#slf4j_compatible




回答2:


You do not have to write your own logging implementation, how about switching the logging framework to logback? Loback supports logging to database natively. As you use the SLF4J API both times your code won't change.

Take a look at the ch.qos.logback.classic.db.DBAppender. If the default table layout does not fit your needs, you can implement ch.qos.logback.classic.db.names.DBNameResolver to customzie the table and coloum names.



来源:https://stackoverflow.com/questions/2653855/implement-custom-logger-with-slf4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!