log4j2 writing method and class

1.9k Views Asked by At

I'm using log4j2 in a java program.. this is the line of code where it is initialized

private static final Logger logger = LogManager.getLogger("application-
log");

and this is the configuration file where the format is decided

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorinterval="30" status="info" strict="true">
<Properties>
    <Property name="logApplicativo">../logs/logApplicativo.log</Property>
    <Property name="logCdr">../logs/logCdr.log</Property>
</Properties>
<Appenders>
    <Console name="STDOUT">
            <PatternLayout pattern="%m MDC%X%n"/>
    </Console>
    <RollingRandomAccessFile   name="fileLogApplicativo" 
    fileName="${logApplicativo}" filePattern="${log-Applicativo}-%d{yyyy-MM-
    dd}-%i.log" immediateFlush="false" append="true" 
    ignoreExceptions="false" >
        <PatternLayout>
            <pattern>%d [%-6p] %C.%M(%F:%L) - %m%n</pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="5 MB"/>
        </Policies>
        <DefaultRolloverStrategy max="30"/>
    </RollingRandomAccessFile >
    <RollingRandomAccessFile  name="fileLogCdr" fileName="${logCdr}" 
    filePattern="${log-Cdr}-%d{yyyy-MM-dd}-%i.log" >
        <PatternLayout>
            <pattern>%d %-5p %c{2} - %m%n</pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="5 MB"/>
        </Policies>
        <DefaultRolloverStrategy max="30"/>
    </RollingRandomAccessFile>
</Appenders>
<Loggers>
    <AsyncRoot level="error">
            <AppenderRef ref="STDOUT"/>
    </AsyncRoot>
    <AsyncLogger name="application-log" level="debug" additivity="false">
        <AppenderRef ref="fileLogApplicativo" level="debug"/>>
    </AsyncLogger>
    <AsyncLogger name="cdr-log" level="debug" additivity="false">
        <appender-ref ref="fileLogCdr" level="debug"/>
    </AsyncLogger>
</Loggers>

I do not understand why, but class name, code name, line code arent printed. Probably the problem is trivial but I'm going crazy from one day.

Thank you all

1

There are 1 best solutions below

0
D.B. On

If you're using asynchronous loggers or asynchronous appenders try adding includeLocation="true" to your logger or appender configuration.

You can read more about it in the log4j2 manual