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的配置,,,讓我笑會兒。。。反正這配置能正常幹活了,如果你們知道真正的正確的配置,求告知!!


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