简体   繁体   中英

log4j2 logging to console only

I have a maven web project which requires logging.

I decided to use log4j2 for that purpose and added the required entries in the pom.xml file

Then I created the log4j2.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <RollingFile name="RollingFile" fileName="${logs.path}/text.log"
            filePattern="${logs.path}/%d{YYYYMMdd}-text.%i.log">
            <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
            <Policies>
                <SizeBasedTriggeringPolicy size="100 MB" />
            </Policies>
            <DefaultRolloverStrategy max="20" />
        </RollingFile>

        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} %-5p %c{1}:%L - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="root" level="info" additivity="false">
            <appender-ref ref="RollingFile" level="info" />
            <appender-ref ref="Console" level="info" />
        </Logger>
        <Root level="info" additivity="false">
            <AppenderRef ref="RollingFile" />
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

I'm starting tomcat with -Dlogs.path="C:\\mylogs" , where C:\\mylogs exists and has public read/write access.

The console logger is working fine, I can see the correct output in my console, the issue is that the file isn't being created, so I have no logfile, everything gets logged to catalina.out only, and the tomcat startup logs don't show errors for log4j.

What am I missing? is there some additional configuration I need to do in order to log to file?

To create a log file with the log4j2 config you first need to define your appender. Here's an example:

<RollingFile name="MGMT"
                 fileName="${logdir}/mgmt.log"
                 filePattern="${logdir}/mgmt.%d{yyyy-MM-dd}.log">
      <PatternLayout pattern="${layout}"/>
      <CronTriggeringPolicy schedule="0 0 0 * * ?"/>
      <DefaultRolloverStrategy>
        <Delete basePath="${logdir}" maxDepth="1">
          <IfFileName glob="mgmt.*.log" />
          <IfAccumulatedFileCount exceeds="10" />
        </Delete>
      </DefaultRolloverStrategy>
    </RollingFile>

Afterwards you just need to define a logger that will use it:

<Logger name="the package or name that is displayed in the log line"
            level="ALL"
            additivity="false">
      <AppenderRef ref="MGMT"/>
    </Logger>

If you have a log that has com.company.test as package set that package as name for the logger. It's important that they match.

The field additivity will define if you want to pass the catched log to its parent ( true ) or just let this logger handle it ( false )

EDIT:

your config file might need this:

<Configuration status="info">
  <Properties>
    <Property name="logdir">${sys:catalina.base}/logs</Property>
    <Property name="layout">%d [%t] %-5p %c- %m%n</Property>
  </Properties>
  <Appenders>

If you don't want to use this you have to define the path static like <RollingFile name="MGMT" fileName="C:\\path\\file.log"

I went through the documentation . You must refer to system properties with sys: , and it seems that tomcat properties are seen as system properties, so I replaced ${logs.path} with ${sys:logs.path} and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM