is it possible to log for all classes, to a single file instead of
final Logger logger = LoggerFactory.getLogger(Wombat.class);
what of you need to log for all classes?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The short answer…
The
Loggeryou use in code does not say where the logging messages are output. (What decides where the messages go is the binding and configuration you use.)Note that messages with the same
Loggerwill be forced to go to the same place since you’ll no longer be able to tell them apart – so you want each class to have its ownLoggerso that you have maximum choice later.If you want to output all logging messages to a single file the option I’d suggest is using the log4j binding along with the second log4j.properties example I’ve given below. For the most part you should be able to copy/paste this to get running and then configure at your leisure.
The long answer…
Covering SLF4J and some of its bindings.
SLF4J
From the website, http://www.slf4j.org/
So it’s all down to what binding you use! In fact the idea is that you don’t even have to settle on a binding until runtime, and don’t need to change any of your logging code to change your mind later!
I’ll discuss the two that I’m familiar with, note there are others and you should consider doing your own research before settling on a particular implementation.
Simple Binding
The simple binding (eg. slfj-simple-1.6.4.jar) has no configuration.
It simply logs all messages of INFO and above to standard error output (System.err).
Log4j Binding
Generally though you want to use a more complex binding such as log4j (which is what I use and so will talk about). It is far more flexable than simple binding, but requires you do do some configuration.
First you’ll require a different binding jar (eg. slf4j-log4j12-1.6.4.jar). You’ll also need the corresponding log4j jar (eg. log4j-1.2.16). And finally you’ll need to configure log4j itself – a log4j.properties file to be located in the root of the project.
I’ll jump to a couple of example log4j.properties files
This first one outputs everything to console, and is pretty similar to what the simple binding achieves, except you can configure the level (TRACE, DEBUG, WARN, INFO, ERROR) that you log at, and the output pattern.
For outputting to a single file you’ll want a different appender, something like:
Log4j is setup around the concept of loggers and appenders.
Loggers (different from the logger class you’ll use with SLF4J) capture messages according to some pattern (such as messages within a particular package), and pipe them to an appender.
Appenders define how the messages are to be output (eg. file, network, console, database), and their format.