Log4j - onwardpath/demo-repo GitHub Wiki
What is logging good for ?
Tracing program execution during development Debugging Providing an audit trail for a program Recording soft-errors (lost connections, etc.) for monitoring performance and troubleshooting A logging framework lets you use different logging levels
Adding logging code
Replaces System.out.println() calls throughout Utility of logging code is improved if it’s consistent—logs can be searched. Five recognized message priorities DEBUG,INFO,WARN,ERROR ,FATAL
Installing Log4J
Download a zip or compressed tarfile from: Apache.org website Uncompress to a local directory In your Eclipse project’s properties Locate Java Build Path On Libraries page, click on Add External Jars Browse for your Log4J directory, and locate the log4J Jar file in the /dist/lib directory Press Open, followed by OK
Create a Log4J configuration file
The easiest way to configure Log4J is to add a log4j.properties file to your source directory. There are many options available which we won’t cover here, but essentially you need to define: A logger An appender A pattern layout The following example uses the default root logger, appends to the console and prints the date, time, message priority, thread and message
Log4j Set up Process
Step 1 - Download the Log4j library
If you are using Maven 2 for your project, you do not need to download the binary. You can simply declare it as a dependency in your Maven 2 pom.xml and Maven will grab the library for you. Declare it as follows
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
Step 2 - Import the jar file into your web project
If your using Maven 2, you can skip this step; after declaring the dependency, Maven will put the jar file in the proper place automatically when you build.
Import the log4j jar file into your JavaBuild Path
Step 3 - Create Log4j Properties file
-
In Eclipse, right click on the src folder
-
Select New → Others → file
-
Create file with name log4j.properties and add below property setting in file
-
# Root logger option
-
log4j.rootLogger=INFO, file •
-
# Direct log messages to a log file
-
log4j.appender.file=org.apache.log4j.RollingFileAppender •
-
#Redirect to Tomcat logs folder
-
#log4j.appender.file.File=${catalina.home}/logs/logging.log •
-
log4j.appender.file.File=C:\\log\\webpersonifyem.log
-
#log4j.appender.file.File=C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\geologs\\webpersonifyem.log
-
log4j.appender.file.MaxFileSize=10MB
-
log4j.appender.file.MaxBackupIndex=10
-
log4j.appender.file.layout=org.apache.log4j.PatternLayout
-
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} UTC %-5p %c{1}:%L - %m%n
-
Make sure before you commit in GitHub log file location need to be changed to server location
Step 5 - Put logging code in your classes
The last thing you need to do is drop some logging code in a class and test this whole setup.
Add the following to the imports section of your java code:
import org.apache.log4j.Logger;
static Logger log = Logger.getLogger(MyClassName.class);
Throw some logging statements in your code somewhere where you know they’ll be fired right away when you run your app.
For example: log.trace("Hello");
log.debug("Hello");
log.info("Hello");
log.error("Hello");
log.warn("Hello");
log.fatal("Hello");
Step 6 - Run your app and make sure it works
Finally, run your app and make sure it works. You should see log lines in your file where you setup your appender. If it doesn’t work, just review these steps a little more carefully and fiddle with it.