I have a logback config where I have one logger that should not inherit the syslog appender that has been added to the root logger. I can't find anywhere in the documentation how to do this.
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
<appender-ref ref="SYSLOG" />
</root>
<logger name="jsonlogger" level="INFO">
<appender-ref ref="SYSLOGJSON" />
</logger>
In this example, I do not want jsonlogger to inherit the syslog appender-ref from root.
Turn off additivity (default true) for your logger:
<logger name="jsonlogger" level="INFO" additivity="false">
<appender-ref ref="SYSLOGJSON" />
</logger>
As the logback-manual describes: http://logback.qos.ch/manual/configuration.html#overrridingCumulativity
If you just want it to not have the SYSLOG appender, but FILE and STDOUT, you'll have to register those at the logger itself as well.
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.
The following Logback configuration, one of the loggers explicitly defines it's appender, another one not:
<configuration scan="true" scanPeriod="60 seconds">
<appender name="AMAZING_APPENDER" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>XXX</pattern>
</encoder>
</appender>
<logger name="org.springframework">
<level value="ERROR" />
</logger>
<logger name="com.company" additivity="false">
<level value="INFO" />
<appender-ref ref="AMAZING_APPENDER"/>
</logger>
</configuration>
What appender would the logger without the explicit appender specification would use, if any at all?
By "logger without the explicit specification" I think you are referring to this: <logger name="org.springframework">.
If so, then there is no appender available for that logger and any log events emitted by classes in the org.springframework (regardless of the log level) will be ignored.
If you tweak your logback.xml slightly to add this: <configuration debug="true"> you'll see a WARN event of this nature ...
No appenders present in context [default] for logger [...]
... emitted when a class in the org.springframework emits a log event at ERROR level.
If you want events from the org.springframework namespace (or more generally, from any namespace other than com.company) to be logged then you should add a <root> configuration. For example:
<root level="INFO">
<appender-ref ref="AMAZING_APPENDER"/>
</root>
If you add a root and point it at AMAZING_APPENDER then you should probably set additive="false" on logger name="com.company" otherwise anything logged from the com.company namespace will be logged twice. So, perhaps your desired configuration will be:
<root level="INFO">
<appender-ref ref="STANDARD_APPENDER"/>
</root>
I am using Configuration Transform extension for use of different settings in Debug and Release build configurations of my Windows Service application. Here is my Release:
<root>
<level value="ALL" />
<appender-ref ref="DebugAppender"/>
<appender-ref ref="SmtpAppender"/>
</root>
Here is my Debug, where I dont want to have SMTP appender, but it does not work (logged events are still send by SMTP):
<root>
<level value="ALL" />
<appender-ref ref="DebugAppender"/>
<appender-ref ref="SmtpAppender" xdt:Transform="Remove"/>
</root>
Solution is simplier than I thought:
<root>
<level value="ALL" />
<appender-ref ref="DebugAppender" />
<appender-ref ref="SmtpAppender" xdt:Locator="Match(ref)" xdt:Transform="Remove"/>
</root>
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.
<?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>