Úvodní stránka > GlassFish, Java, Logging > SLF4J, Log4J 2, LogMX – logování

SLF4J, Log4J 2, LogMX – logování

2013-06-02
  • SLF4J is simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time.
  • Log4J 2 is an upgrade to Log4j that provides significant improvements
  • LogMX is an intuitive and easy-to-use cross platform graphical tool for developers and administrators working with log and trace files.
  1. Stáhni SLF4J a rozbal následující knihovny do: GLASSFISH_HOME\glassfish\lib\
    • jul-to-slf4j.jar
    • slf4j-api.jar
  2. Vytvoř soubor GLASSFISH_HOME\glassfish\domains\dev\config\slf4j_logging.properties:
            handlers = org.slf4j.bridge.SLF4JBridgeHandler
            com.sun.enterprise.server.logging.GFFileHandler.flushFrequency=1
            com.sun.enterprise.server.logging.GFFileHandler.file=${com.sun.aas.instanceRoot}/logs/server.log
            com.sun.enterprise.server.logging.GFFileHandler.rotationTimelimitInMinutes=0
            com.sun.enterprise.server.logging.GFFileHandler.logtoConsole=false
            com.sun.enterprise.server.logging.GFFileHandler.rotationLimitInBytes=2000000
            com.sun.enterprise.server.logging.GFFileHandler.alarms=false
            com.sun.enterprise.server.logging.GFFileHandler.formatter=com.sun.enterprise.server.logging.UniformLogFormatter
            com.sun.enterprise.server.logging.GFFileHandler.retainErrorsStasticsForHours=0
            
  3. Nastav cestu v java property k tomuto configuráku
    • Buď edituj GLASSFISH_HOME\glassfish\domains\dev\config\domain.xml:
                      <jvm-options ... >
                      ...
                      -Djava.util.logging.config.file=${com.sun.aas.instanceRoot}/config/slf4j_logging.properties
                      ...
                      </jvm-options>
                      
    • Nebo pomocí Glassfish asadmin
  4. Stáhni Log4J2 a rozbal následující knihovny do: GLASSFISH_HOME\glassfish\lib\
    • log4j-api.jar
    • log4j-core.jar
  5. Vytvoř konfigurační soubor log4j2.xml
            <?xml version='1.0' encoding='UTF-8'?>
            <configuration name="SpringWS" monitorInterval="5">
    
                <properties>
                    <property name="pattern1">%d %p %c [%t] %m%n</property>
                    <property name="pattern2">%d %p %c{1.} [%t] %m%n</property>
                </properties>
    
                <appenders>
                    <RollingFile name="RollingFile"
                                 fileName="c:/java/logs/my_app.log"
                                 filePattern="c:/java/logs/$${date:yyyy-MM}/my_app-%d{yyyy-MM-dd}-%i.log.gz">
                        <PatternLayout>
                            <pattern>${pattern1}</pattern>
                        </PatternLayout>
                        <Policies>
                            <TimeBasedTriggeringPolicy/>
                            <SizeBasedTriggeringPolicy size="100 KB"/>
                        </Policies>
                    </RollingFile>
                </appenders>
    
                <loggers>
                    <!--
                    <root level="trace">
                        <appender-ref ref="RollingFile"/>
                    </root>
                    -->
                    <!-- name must be package or package.Class -->
                    <logger name="example" level="trace" additivity="false">
                        <appender-ref ref="RollingFile"/>
                    </logger>
                </loggers>
    
            </configuration>
            
  6. A opět java property k tomuto configuráku
    • GLASSFISH_HOME\glassfish\domains\dev\config\domain.xml:
                      <jvm-options... >
                      ...
                      -Dlog4j2.configurationFile=${com.sun.aas.instanceRoot}/config/log4j2.xml
                      ...
                      </jvm-options>
                      
    • Nebo pomocí Glassfish asadmin
  7. Přidej Log4J 2 závislost do pom.xml:
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.0-beta6</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.0-beta6</version>
                <scope>provided</scope>
            </dependency>
            
  8. Přidej logger do kódu:
  9.         import org.apache.logging.log4j.LogManager;
            import org.apache.logging.log4j.Logger;
    
            public class MyClass {
                Logger logger = LogManager.getLogger(MyClass.class.getName());
    
                public void someMethod() {
    
                    logger.trace("Entering method {0}.", someProperty);
            
  10. Restartuj server
  11. Stáhni nějaký log viewer, např. LogMX
    LogMX parser setting
Reklamy
Rubriky:GlassFish, Java, Logging