I tried to add a new log file in my project, I updated the logback.xml but when I test the new log file is not created but the log message is placed in another log file.
logback.xml :
<appender name="myLogFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APPL_FULL_PATH}/myLogFile.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APPL_FULL_PATH}/myLogFile.log.%d{yyyy-MM-dd}</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy.MM.dd HH:MM:ss} %p %m%n</pattern>
</encoder>
</appender>
<logger name="com.package1" level="INFO" additivity="false">
<appender-ref ref="myLogFile" />
</logger>
<logger name="com.package2" level="INFO" additivity="false">
<appender-ref ref="myLogFile" />
</logger>
In the Java File each time I call the logger by :
private static final Logger myLog = LoggerFactory.getLogger("myLogFile");
myLog.info("Test Log File");
Related
I want to add new log file myNewLog.log, I wont to use different logger level in different environment without infect the existing log. The levels of the dev environment and the prod environment are mentioned in below description :
for Dev env
level error is active
level info is active
level debug is active (default level for the dev)
I added a new appender myNewLog and I setted the default level to DEBUG then I create a 3 loggers with name myNewLog each one has a different level.
logback.xml for the dev env:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="2 seconds">
<property resource="logback.properties" />
<appender name="apiLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APPL_FULL_PATH}/logs/mylog.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APPL_FULL_PATH}/logs/mylog.log.%d{yyyy.MM.dd}</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy.MM.dd HH:mm:ss} %p %m%n</Pattern>
</encoder>
</appender>
<appender name="myNewLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APPL_FULL_PATH}/logs/myNewLog.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APPL_FULL_PATH}/logs/myNewLog%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<Pattern>%d{yyyy.MM.dd HH:mm:ss} %p %m%n</Pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<logger name="apiLog" level="INFO" additivity="false">
<appender-ref ref="apiLog" />
</logger>
<logger name="myNewLog" level="DEBUG" additivity="true">
<appender-ref ref="myNewLog" />
</logger>
<logger name="myNewLog" level="INFO" additivity="false">
<appender-ref ref="myNewLog" />
</logger>
<logger name="myNewLog" level="ERROR" additivity="false">
<appender-ref ref="myNewLog" />
</logger>
<root level="ERROR">
<appender-ref ref="console" />
</root>
</configuration>
for Prod env
level error is active (default level for the prod)
level info will be activated in needs
level debug is deactivated
I added a new appender myNewLog and I setted the default level to ERROR then I create a 3 loggers with name myNewLog each one has a different level.
logback.xml for Prod env :
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="2 seconds">
<property resource="logback.properties" />
<appender name="apiLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APPL_FULL_PATH}/logs/mylog.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APPL_FULL_PATH}/logs/mylog.log.%d{yyyy.MM.dd}</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy.MM.dd HH:mm:ss} %p %m%n</Pattern>
</encoder>
</appender>
<appender name="myNewLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APPL_FULL_PATH}/logs/myNewLog.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APPL_FULL_PATH}/logs/myNewLog%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<Pattern>%d{yyyy.MM.dd HH:mm:ss} %p %m%n</Pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<logger name="apiLog" level="INFO" additivity="false">
<appender-ref ref="apiLog" />
</logger>
<logger name="myNewLog" level="ERROR" additivity="true">
<appender-ref ref="myNewLog" />
</logger>
<logger name="myNewLog" level="DEBUG" additivity="false">
<appender-ref ref="myNewLog" />
</logger>
<logger name="myNewLog" level="INFO" additivity="false">
<appender-ref ref="myNewLog" />
</logger>
<root level="ERROR">
<appender-ref ref="console" />
</root>
</configuration>
My questions are :
how can I manage log level for each environment ?
can I manage the levels for the 2 environments in one logback file ?
in application.yml, use different profile for each environment. and define level var my.log.level
logging:
config: classpath:logback.xml
--- #dev
spring:
profiles:
- dev
my:
log:
level: DEBUG
--- #prod
spring:
profiles:
- prod
my:
log:
level: ERROR
in logback.xml use "<springProperty>" to import this var.
if logback.xml is not effective, please rename it to logback-spring.xml to try again.
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="2 seconds">
<!-- import var from application.yml -->
<springProperty scope="context" name="logLevel" source="my.log.level" defaultValue="INFO"/>
<property resource="logback.properties" />
<appender name="apiLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APPL_FULL_PATH}/logs/mylog.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APPL_FULL_PATH}/logs/mylog.log.%d{yyyy.MM.dd}</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy.MM.dd HH:mm:ss} %p %m%n</Pattern>
</encoder>
</appender>
<appender name="myNewLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APPL_FULL_PATH}/logs/myNewLog.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APPL_FULL_PATH}/logs/myNewLog%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<Pattern>%d{yyyy.MM.dd HH:mm:ss} %p %m%n</Pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${logLevel}</level>
</filter>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>${logLevel}</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<logger name="apiLog" level="INFO" additivity="true">
<appender-ref ref="apiLog" />
</logger>
<logger name="myNewLog" level="ERROR" additivity="false">
<appender-ref ref="myNewLog" />
</logger>
<logger name="myNewLog" level="DEBUG" additivity="true">
<appender-ref ref="myNewLog" />
</logger>
<logger name="myNewLog" level="INFO" additivity="true">
<appender-ref ref="myNewLog" />
</logger>
<root level="ERROR">
<appender-ref ref="console" />
</root>
</configuration>
use --spring.profiles.active to run. for instance, use dev to run
java -jar [mvn-created-jar-file-name] --spring.profiles.active=dev
I'm trying to get separate log file based on log levels, for eg. separate file for debug, info, error
My current configuration in application.properties file only keeps in single file, per date
logging.file=myservice-%d{yyyyMMdd}.log
logging.level.org.springframework.boot=DEBUG
logging.level.org.springframework.web=DEBUG
I want separate log files created as
myservice-info-20190516.log
myservice-debug-20190516.log
myservice-error-20190516.log
You can easily do that with Logback. You can create different appenders for each log level and you're done.
Using the recommended and default implementation Logback of the SLF4J façade, you need to configure appender for each level of the log.
<appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>debug-%d{yyyy-MM-dd_HH}.log</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>info-%d{yyyy-MM-dd_HH}.log</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>error-%d{yyyy-MM-dd_HH}.log</fileNamePattern>
</rollingPolicy>
</appender>
And attach them to the logger distinguished by the level:
<logger name="com.bla" level="DEBUG" additivity="true">
<appender-ref ref="debugAppender"/>
</logger>
<logger name="com.bla" level="INFO" additivity="true">
<appender-ref ref="infoAppender"/>
</logger>
<logger name="com.bla" level="ERROR" additivity="true">
<appender-ref ref="errorAppender"/>
</logger>
I have configured logback.xml to change log level during build time using "scan" property. Using this I can change log level without rebuilding the code. I can change log level by updating logback.xml file
<?xml version="1.0" encoding="UTF-8"?><!--
For more configuration information and examples see
http://logback.qos.ch/manual/configuration.html-->
<configuration scan="true" scanPeriod="10 seconds">
<!--<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />-->
<!-- Debugging appender (duplicates the normal log, PLUS any debug messages) -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %level{5} %c{3} --- %message%n</pattern>
</encoder>
</appender>
<!---->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./log/elk-file.log</file>
<append>true</append>
<!--cleanHistoryOnStart>true</cleanHistoryOnStart-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>./log/elk-file_%d{yyyyMMdd}-%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>200MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>1</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date{ISO8601} [%thread] %level{5} %c{3} - %message%n</pattern>
</encoder>
</appender>
<!-- Our logger writes to file, console and sends the data to Logstash -->
<logger name="ro.fortsoft.elk.testdata" level="DEBUG" additivity="false">
<appender-ref ref="STASH"/>
</logger>
<logger level="INFO" name="rollingFileLogger">
<appender-ref ref="FILE" />
</logger>
<!-- ROOT logger setup -->
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
</root>
How can I do this in Jenkins, i-e change logback.xml and change log level without rebuilding the code.
I have the following logback XML configuration, however when I load my logger:
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger("com.nordea.icelink.adaptiv");
And do and info log by dooing
LOGGER.info(s);
Nothing gets outputed to the file only on the console? How can this be?
<configuration>
<logger name="org.springframework" level="WARN"/>
<logger name="org.eclipse.jetty" level="WARN"/>
<logger name="com.nordea.icelink.adaptiv" level="info">
<appender-ref ref="FILE"/>
</logger>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>../logs/NordeaIceLinkServer.log</file>
<encoder>
<pattern>%date{YYYY-MM-dd HH:mm} %logger{36} %level %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>../logs/NordeaIceLinkServer.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>10MB</maxFileSize>
</triggeringPolicy>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%logger{36} %level %msg%n</pattern>
</encoder>
<root level="debug">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
You should not use a relative path for the log file.
<file>../logs/NordeaIceLinkServer.log</file>
Use an absolute one instead.
<file>/path/logs/NordeaIceLinkServer.log</file>
Actually the appender must be created before the logger. then it worked.
I am using logback/slf4j to do my logging. I want to parse my log file to analyze some data, so instead of parsing a great big file (mostly consisting of debug statements) I want to have two logger instances which each log to a separate file; one for analytics and one for all purpose logging. Does anyone know if this is possible with Logback, or any other logger for that matter?
It's very possible to do something like this in logback. Here's an example configuration:
<?xml version="1.0"?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logfile.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<appender name="ANALYTICS-FILE" class="ch.qos.logback.core.FileAppender">
<file>analytics.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<!-- additivity=false ensures analytics data only goes to the analytics log -->
<logger name="analytics" level="DEBUG" additivity="false">
<appender-ref ref="ANALYTICS-FILE"/>
</logger>
<root>
<appender-ref ref="FILE"/>
</root>
</configuration>
Then you'd setup two separate loggers, one for everything and one to log analytics data like so:
Logger analytics = LoggerFactory.getLogger("analytics");
You can have as many loggers as you wish. But, it's better you have one for each package that you need to log differently. Then all the classes in that package and its sub-packages will get the that specific logger. They all can share the root logger and send their log data to root logger appender using additivity="true". Here's an example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="pattern" value="%date{HH:mm:ss.SSS} %-5p %logger{36}
%X{akkaSource} [%file:%line] - %m%n" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{HH:mm:ss.SSS} %-5p %logger{36} %X{akkaSource} [%file:%line] - %m%n</pattern>
</encoder>
</appender>
<appender name="abc" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/worker.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/worker-%d{yyyy-MM-dd_HH}.log</fileNamePattern>
<maxHistory>360</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${pattern}</pattern>
</encoder>
</appender>
<appender name="xyz" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/transformer.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/transformer-%d{yyyy-MM-dd_HH}.log</fileNamePattern>
<maxHistory>360</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${pattern}</pattern>
</encoder>
</appender>
<logger name="com.xxx.yyy.zzz" level="INFO" additivity="true">
<appender-ref ref="xyz"/>
</logger>
<logger name="com.aaa.bbb.ccc" level="INFO" additivity="true">
<appender-ref ref="abc"/>
</logger>
<root>
<level value="INFO" />
<appender-ref ref="STDOUT" />
</root>
in my case I wanted to leave class names as log name
private static final Logger log = LoggerFactory.getLogger(ScheduledPost.class);
and as I had few such classes, so my logback.xml
<!--additivity=false ensures this log data only goes to the this log, and no one more -->
<logger name="xxx.xxx.xxx.ScheduledPost" level="DEBUG" additivity="false">
<appender-ref ref="ASYNC_SCHEDULE_LOG_FILE"/>
</logger>
<logger name="xxx.xxx.xxx.GcmPost" level="DEBUG" additivity="false">
<appender-ref ref="ASYNC_SCHEDULE_LOG_FILE"/>
</logger>
<logger name="xxx.xxx.xxx.PushUtils" level="DEBUG" additivity="false">
<appender-ref ref="ASYNC_SCHEDULE_LOG_FILE"/>
</logger>