Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
844 views
in Technique[技术] by (71.8m points)

java - Implement Custom Logger with slf4j

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:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...