My story:
I want to make a thing which is as simple as a simplest possible log4j logger that logs rows to a file. I have found several examples with some functionality, but not a basic, general one that really works, and not one with an explanation how the each row work.
Question:
Could anybody provide one?
Prerequisites:
- I already know where to put the file and I have the log4j configured and working for console logging.
- Now I want to log to a file and also find the file from file system once the program has run.
- Rows needed to be added to the existing
log4j.properties
file are the desired output.
Here's a simple one that I often use:
The format of the log is as follows:
Such a format is defined by the string
%5p\t[%d] [%t] (%F:%L)\n \t%m%n\n
. You can read the meaning of conversion characters in log4j javadoc forPatternLayout
.Included comments should help in understanding what it does. Further notes:
owls_conditions.log
: change it according to your needs;I have one generic log4j.xml file for you:
with one console, two file appender and one logger poiting to the second file appender instead of the first.
EDIT
In one of the older projects I have found a simple log4j.properties file:
For the description of all the layout arguments look here: log4j PatternLayout arguments
Here is a log4j.properties file that I've used with great success.
The DailyRollingFileAppender will create new files each day with file names that look like this:
Each entry in the log file will will have this format:
Set the location of the above file by using
-Dlog4j.configuration
, as mentioned in this posting:In your Java code, be sure to set the name of each software component when you instantiate your logger object. I also like to log to both the log file and standard output, so I wrote this small function.
And then call it like so:
Log4j can be a bit confusing. So lets try to understand what is going on in this file: In log4j you have two basic constructs appenders and loggers.
Appenders define how and where things are appended. Will it be logged to a file, to the console, to a database, etc.? In this case you are specifying that log statements directed to fileAppender will be put in the file
sample.log
using the pattern specified in the layout tags. You could just as easily create a appender for the console or the database. Where the console appender would specify things like the layout on the screen and the database appender would have connection details and table names.Loggers respond to logging events as they bubble up. If an event catches the interest of a specific logger it will invoke its attached appenders. In the example below you have only one logger the root logger - which responds to all logging events by default. In addition to the root logger you can specify more specific loggers that respond to events from specific packages. These loggers can have their own appenders specified using the
appender-ref
tags or will otherwise inherit the appenders from the root logger. Using more specific loggers allows you to fine tune the logging level on specific packages or to direct certain packages to other appenders.So what this file is saying is:
The net out is that if you have a
logger.debug("blah blah")
in your code it will get ignored. Alogger.info("Blah blah");
will output to sample.log.The snippet below could be added to the file above with the log4j tags. This logger would inherit the appenders from
<root>
but would limit the all logging events from the packageorg.springframework
to those logged at levelinfo
or above.