Getting the log4j jars using maven
The pom file
1: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"2: xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">3: <modelVersion>4.0.0</modelVersion>4:5: <groupId>com.mycompany</groupId>6: <artifactId>Log4jTest</artifactId>7: <version>1.0-SNAPSHOT</version>8: <packaging>jar</packaging>9: <name>Log4jTest</name>10: <url>http://maven.apache.org</url>11:12: <properties>13: <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>14: </properties>15:16: <dependencies>17:18: <dependency>19: <groupId>log4j</groupId>20: <artifactId>log4j</artifactId>21: <version>1.2.17</version>22: </dependency>23: </dependencies>24: </project>25:
The log4j configuration file.
Note: 2 appeanders are declared one for the output of logging into the console and one for output it to file.
The appeanders to use in each call to logging are declared
1: <?xml version="1.0" encoding="UTF-8" ?>2: <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">3: <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>4:5: <appender name="default.file" class="org.apache.log4j.FileAppender">6: <param name="file" value="/log/mylogfile.log" />7: <param name="append" value="false" />8: <param name="threshold" value="debug" />9: <layout class="org.apache.log4j.PatternLayout">10: <param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />11: </layout>12: </appender>13:14: <appender name="default.console" class="org.apache.log4j.ConsoleAppender">15: <param name="target" value="System.out" />16: <param name="threshold" value="debug" />17: <layout class="org.apache.log4j.PatternLayout">18: <param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />19: </layout>20: </appender>21:22: <root>23: <priority value="info" />24: <appender-ref ref="default.file" />25: <appender-ref ref="default.console" />26:27: </root>28: </log4j:configuration>
The main file
The main file the demonstarts the use of logging.
Note about the configuration laoding using the DomConfigurator object
1: package com.mycompany.log4jtest;2:3: import org.apache.log4j.LogManager;4: import org.apache.log4j.Logger;5: import org.apache.log4j.xml.DOMConfigurator;6:7: public class App8: {9: private static Logger logger = LogManager.getLogger("myLogger");10:11: public static void main( String[] args )12: {13: DOMConfigurator.configure("log4j.xml");14: logger.info("This is an info!");15: logger.error("This is an error!");16: System.out.println( "Done!" );17: }18: }19:
The output
------------------------------------------------------------------------
Building Log4jTest 1.0-SNAPSHOT
------------------------------------------------------------------------
[resources:resources]
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
skip non existing resourceDirectory I:\Learn\java\Log4jTest\src\main\resources
[compiler:compile]
Compiling 1 source file to I:\Learn\java\Log4jTest\target\classes
[exec:exec]
2013-06-01 22:48:06,945 INFO [myLogger] - This is an info!
2013-06-01 22:48:06,946 ERROR [myLogger] - This is an error!
Done!
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.313s
Finished at: Sat Jun 01 22:48:06 IDT 2013
Final Memory: 6M/15M
------------------------------------------------------------------------
sources
http://stackoverflow.com/questions/6358836/log4j-how-to-configure-simplest-possible-file-logging
http://www.dzone.com/tutorials/java/log4j/log4j-file-appender-example-1.html
אין תגובות:
הוסף רשומת תגובה