Vert.x日志配置

参考资料:官网
1.默认jdk的JUL日志
(1)设置系统变量 key = java.util.logging.config.file
                      value = logging日志的配置文件名(classPath:myLogging.properties)
(2)更方便的方式是:在classpath路径下提供默认日志文件
                    vertx-default-jul-logging.properties

官网配置如下:

# Copyright 2014 Red Hat, Inc.
#
#  All rights reserved. This program and the accompanying materials
#  are made available under the terms of the Eclipse Public License v1.0
#  and Apache License v2.0 which accompanies this distribution.
#
#  The Eclipse Public License is available at
#  http://www.eclipse.org/legal/epl-v10.html
#
#  The Apache License v2.0 is available at
#  http://www.opensource.org/licenses/apache2.0.php
#
#  You may elect to redistribute this code under either of these licenses.
#
handlers=java.util.logging.ConsoleHandler,java.util.logging.FileHandler
java.util.logging.SimpleFormatter.format=%5$s %6$s\n
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.FileHandler.level=INFO
java.util.logging.FileHandler.formatter=io.vertx.core.logging.impl.VertxLoggerFormatter

# Put the log in the system temporary directory
java.util.logging.FileHandler.pattern=%t/vertx.log

.level=INFO
io.vertx.ext.web.level=FINEST
io.vertx.level=INFO
com.hazelcast.level=INFO
io.netty.util.internal.PlatformDependent.level=SEVERE

虽然官网上这么写,上面的方法,反正我试了N次也没有找到日志文件,我靠~~~~~~~~~~~~~~~~~~~




2.配置log4j日志

        设置系统变量 key = vertx.logger-delegate-factory-class-name
                   value = 实现了LogDelegateFactory接口的类
        如:io.vertx.core.logging.Log4jLogDelegateFactory
               io.vertx.core.logging.Log4j2LogDelegateFactory

               io.vertx.core.logging.SLF4JLogDelegateFactory


配置方法:

(1)在pom中添加log4j,slf4j相关依赖

	<!--log4j-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j2.version}</version>
        </dependency>


(2)在src/main/resources下增加log4j.xml日志配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
    <Appenders>
        <RollingFile name="app_file" append="true" fileName="log/seachlog.log" filePattern="log/vertx/$${date:yyyy-MM}/seachlog-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d{ISO8601} %-5p %c:%L - %m%n" />
            <Policies>
                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="5MB" />
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
        <RollingFile name="vertx_file" append="true" fileName="log/vertx.log" filePattern="log/vertx/$${date:yyyy-MM}/vertx-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d{ISO8601} %-5p %c:%L - %m%n" />
            <Policies>
                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="5MB" />
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <!-- <LogStashJSONLayout/> -->
            <PatternLayout pattern="%d{ISO8601} %-5p %c:%L - %m%n" />
        </Console>
    </Appenders>

    <Loggers>
        <Logger name="io.vertx.core" level="DEBUG">
            <!-- <AppenderRef ref="vertx_rollingFile" /> -->
            <!-- <AppenderRef ref="vertx_socket" /> -->
            <AppenderRef ref="STDOUT"/>
            <AppenderRef ref="vertx_file" />
        </Logger>
        <Root level="DEBUG">
            <!-- <AppenderRef ref="vertx_socket" /> -->
            <AppenderRef ref="STDOUT"/>
            <AppenderRef ref="app_file" />
        </Root>
    </Loggers>
</Configuration>

【注意:】在使用 Logger logger = LoggerFactory.getLogger(this.getClass()); 时,采用slf4j的日志,而不是core中的logger

	import org.slf4j.Logger;
	import org.slf4j.LoggerFactory;

【附录:】  

 log4j配置说明相关文档

等会儿,为什么我没有用到“设置系统变量”,为什么这个log4j的配置这么像Spring的配置,,,让我笑会儿。。。反正这配置能正常干活了,如果你们知道真正的正确的配置,求告知!!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章