Log4j2 use a custom logger, the log does not print out the class name _log4j/log4j2

Source: Internet
Author: User
Tags log4j

2017-08-29 Update:

20:02:

<asynclogger name= "lottery" level= "info" additivity= "false" includelocation= "true" >
<appenderref ref= "Lotterylog"/>
</AsyncLogger>

Using asynchronous logging requires includelocation= "true" to solve the problem of log details such as no class name and number of rows

However, according to the official website description, this seems to lose the advantages of asynchronous performance http://logging.apache.org/log4j/2.x/manual/appenders.html

Extracting location is a expensive operation (it can make logging 5-20 times slower). T

o Improve performance, location is isn't included by default if adding a log event to the queue.

You can change this by setting includelocation= "true".



The problem of asynchronous logger when found,

Change to <logger name= "lottery" level= "info" additivity= "false" >
<appenderref ref= "Lotterylog"/>
</Logger>

Just fine ...


Found GetLogger () incoming class, always able to play the class name

If you use the incoming Logmanager.getlogger (class name) directly, you can also play it, but you cannot play it by nesting one layer using the wrapper class

Logutil.get (class name)

public static Logger Get (String name) {
return Logmanager.getlogger (name);
}

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

If you want to see the specific class and number of rows, or pass in class classes, and then change the name of the custom logger to the corresponding package name or class name

The class that needs to be consolidated is placed under the same package, with the XXXX.XXX package name for the logger definition, which will be played together because of the parent relationship


Configuration file:

<rollingrandomaccessfile name= "Lotterylog" filename= ${log_home}/lottery.log "filepattern=" ${LOG_HOME}/
                                 Lottery.%d{yyyy-mm-dd}-%i.log.gz "append=" true ">
            <patternlayout pattern=" [Main][%date{yyyy-mm-dd HH:mm: Ss. sss}][%thread][%level][%class][%line]:%message%n "/>
            <Filters>
                <thresholdfilter level=" Info " Onmatch= "ACCEPT" onmismatch= "NEUTRAL"/>
            </Filters>
            <Policies>
                < Timebasedtriggeringpolicy interval= "1" modulate= "true"/>
            </Policies>
            < Defaultrolloverstrategy max= "/>"
        </RollingRandomAccessFile>

<asynclogger name= "lottery" level= "info" additivity= "false" >
            <appenderref ref= "Lotterylog"/>
        </AsyncLogger>

Java:

Private final static Logger Logger = Logutil.get (drawprizeremoteserviceimpl.class);
    Private final static Logger log2 = Logmanager.getlogger ("lottery");
    Private static final Org.slf4j.Logger log = Loggerfactory.getlogger ("lottery");


Can print the log file, but it is found that in addition to the GetLogger method passing in the class or the incoming class name can find a matching logger in the log4j configuration file,

The remaining printed log files are question marks in the class name (%class) and the number of rows (%line)

[Main] [2016-12-27 00:00:00.003] [pigeon-server-request-processor-34-thread-49] [INFO] [?] [] XXXXXXX


Full information is only played when a class is used as a parameter or if a logger with a full class name matches.

[2017-08-29 14:13:26.630] [Pigeon-server-request-processor-34-thread-3] [INFO] [XXXXXXXX. DRAWPRIZEREMOTESERVICEIMPL][89]:d ASDSADASDASDASD


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.