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?
The Logger you 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 Logger will 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 own Logger so 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.
Covering SLF4J and some of its bindings.
From the website, http://www.slf4j.org/
The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time.
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.
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).
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.
# Root Logger : TRACE & above to console appender
log4j.rootLogger=TRACE, console
# Appender : Console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-8r %-5p %c [%t] %m%n
For outputting to a single file you'll want a different appender, something like:
# Root Logger : WARN & above to mainFile appender
log4j.rootLogger=WARN,mainFile
# Appender : mainFile
log4j.appender.mainFile=org.apache.log4j.FileAppender
log4j.appender.mainFile.File=main.log
log4j.appender.mainFile.layout=org.apache.log4j.PatternLayout
log4j.appender.mainFile.layout.ConversionPattern=%-8r %-5p %c [%t] %m%n
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With