<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{HH:mm:ss} %-5level %logger{35} - %msg%n
</Pattern>
</encoder>
</appender>
<logger name="java.sql" level="DEBUG" >
<appender-ref ref="STDOUT" />
</logger>
<root>
<level value="ERROR" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
I want print sql and error only,but it does not print SQL in Console,can any one help me? thanks lot
You configuration of appender is correct but logger configuration is not.
To log SQL statements for particular mybatis mapper set DEBUG (TRACE to see query parameters and results) level for logger with fully qualified mapper name
<logger name="com.mycompany.myapp.mapper.MyMapper" level="DEBUG"/>
You can log all SQL statements from all mappers if they are in the same package like this
<logger name="com.mycompany.myapp.mapper" level="DEBUG"/>
<logger name="org.mybatis">
<level value="TRACE"/>
</logger>
<logger name="java.sql">
<level value="WARN"/>
</logger>
I used the above code snippet in my logback.xml and got the sql query in logger. If require to print sql in specific logger, use tag.
thanks, I add to logback.xml
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logFile}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logFile}.%d.zip</fileNamePattern>
<maxHistory>5</maxHistory>
</rollingPolicy>
<encoder>
<pattern>[%date] [%level] %msg [%file:%line] [%logger{100}] [%thread] [AspLink-${project.version}]%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[AspLink-${project.version}] [%level] %msg [%file:%line] [%logger{100}]%n</pattern>
</encoder>
</appender>
<logger name="liquibase" level="INFO" />
<logger name="ru.company.asplink.dao" level="DEBUG" />
<logger name="org.mybatis">
<level value="TRACE"/>
</logger>
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
Related
I am trying to display logs into console as well as file however in file i want only certain logs that will be displayed from one particular class and i am not sure how to do this. Below is my logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>{ "date_time":"%date", "thread":"[%thread]", "log_level":"%-5level", "class_name":"%logger{0}", "log_message":"%msg" }%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file> test.txt </file>
<append>false</append>
<encoder>
<pattern>{ "date_time":"%date", "thread":"[%thread]", "log_level":"%-5level", "class_name":"%logger{0}", "log_message":"%msg" }%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
Just declare a logger for your "one particular class" and then associate that logger with your FILE appender.
For example:
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>test.log</file>
<encoder>
<pattern>{ "date_time":"%date", "thread":"[%thread]", "log_level":"%-5level", "class_name":"%logger{0}", "log_message":"%msg" }%n</pattern>
</encoder>
</appender>
<logger name="your.particular.Class">
<appender-ref ref="FILE" />
</logger>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
This will ensure that any log events emitted by your.particular.Class are only directed to the FILE appender and all other logs will be directed only to the STDOUT appender.
I have this simple Logback config file, containing two appenders and some custom logging levels based on package name.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>%date{HH:mm}\t%-5level\t%msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>my_logger.log</file>
<encoder>
<charset>UTF-8</charset>
<pattern>%date{dd MMM HH:mm}|%class{0}|%-5level|%msg%n</pattern>
</encoder>
</appender>
<!-- custom logging levels -->
<logger name="myapp.package1" level="INFO" />
<logger name="myapp.package2" level="INFO" />
<root>
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
With this configuration, the custom logging levels are applied for both appenders.
How can I change it so that only ConsoleAppender uses these custom logging levels, while FileAppender sticks to default levels?
If you don't want to implement a custom filter, you can create a new appender with a fixed threshold (in you case INFO):
<appender name="INFO_CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
...
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
</appender>
Then, for your custom logging levels add the INFO_CONSOLE and your FILE appender. The additivity="false" attribute prevents the logger from logging to the CONSOLE appender inherited from root.
<logger name="myapp.package1" additivity="false">
<appender-ref ref="INFO_CONSOLE" />
<appender-ref ref="FILE" />
</logger>
This should log DEBUG and above to the FILE and CONSOLE appenders, except for myapp.package1 which will only log INFO and above to CONSOLE.
I'm trying to configure logback so that certain packages are logged differently depending on the appender. I have a ConsoleAppender and a RollingFileAppender. I want TRACE level logging for the ConsoleAppender and I want only two packages logged to the RollingFileAppender. What ends up happening is my ConsoleAppender and RollingFileAppender behave exactly the same.
Here is my logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<property resource="logback.properties" />
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>.%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %n</pattern>
</encoder>
</appender>
<appender name="dailyRollingFileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logback.dir}${logback.filename}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${logback.dir}${logback.filename}.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 90 days' worth of history -->
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="INFO">
<appender-ref ref="dailyRollingFileAppender" />
</logger>
<logger name="x.y" level="DEBUG">
<appender-ref ref="dailyRollingFileAppender" />
</logger>
<root>
<level value="TRACE" />
<appender-ref ref="consoleAppender" />
</root>
</configuration>
I do not know why logback writes log into the console, please help thanks.
<configuration debug="true" scan="true">
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/log/%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
%msg%n</pattern>
</encoder>
</appender>
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="FILE" />
<includeCallerData>true</includeCallerData>
</appender>
<root level="INFO">
<appender-ref ref="ASYNC" />
</root>
<root level="ERROR">
<appender-ref ref="ASYNC" />
</root>
</configuration>
You have named both your appender the same ref="FILE" and your root level loggers as "ASYNC"
i am using logback in my java web appliaction. here's my "logback.xml" file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<property name="LOG_DIR" value="/home/ying/.jetty_logs/vehicle" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%thread] %-5level %logger{36}[%L] - %msg%n</pattern>
</encoder>
</appender>
<appender name="LAST" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/last.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>last.%d{yyyy-MM}.gz</fileNamePattern>
<maxHistory>24</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date:%msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.apache.shiro" level="WARN" />
<logger name="org.hibernate" level="WARN" />
<logger name="ying.car.interceptor.AutoLoginInterceptor" additivity="false" level="INFO">
<appender-ref ref="LAST" />
</logger>
<logger name="ying.car.controller.LoginController" additivity="false" level="INFO">
<appender-ref ref="LAST" />
</logger>
<logger name="ying.car.controller.LogoutController" additivity="false" level="INFO">
<appender-ref ref="LAST" />
</logger>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
today is june 1st 2013, all my old logs are overwrited and no *.gz created.
somebody help me, please.
Try to do like this, Hope it will work for you.
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGDIR}/filename.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<FileNamePattern>${LOGDIR}/file.%d{yyyy-MM-dd}.%i.log.gz
</FileNamePattern>
<!-- keep 30 days' worth of history -->
<MaxHistory>30</MaxHistory>
<!-- or whenever the file size reaches 10MB -->
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%date [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
</appender>
The above code will compress your file on the day basis or If the log file size exceeds 10MB.
Note : I have added "%i" in filePattern, It will iterate your file name as file1,file2 etc.
Try adding <cleanHistoryOnStart>true</cleanHistoryOnStart> so that the rollover happens on next startup, as if it wasn't running at the time of rollover it wouldn't have happened.