Ignore logging messages from dependencies [duplicate] - java

In my application I use Java, Hibernate.
Logging : I use logback.xml
Can anyone suggest if there is a way to disable the logs from the below specific class from Hibernate jar.
LOGGER to be removed from the specific class : ERROR o.h.e.jdbc.spi.SqlExceptionHelper
logback.xml:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<logger name="org.springframework" level="error"
additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>

Add the following to your logback.xml configuration file:
<logger name="org.hibernate.engine.jdbc.spi.SqlExceptionHelper" level="OFF"/>
The instruction: level="OFF" tells Logback to disable all log output for a given logger.

In your logback.xml configuration adding the following element should work
<logger name="org.hibernate.engine.jdbc.spi.SqlExceptionHelper" level="OFF"/>

Related

JNDI (in Logback.xml)

I have the following file:
Logback.xml
<configuration>
<insertFromJNDI scope="context" env-entry-name="java:jboss/properties/mypropertyfile" as="propertyfile" />
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
${mypattern1} %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<logger name="com.mkyong" level="debug" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<root level="error">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
I want to reference variables in "mypropertyfile.properties" which is referenced via the insertFromJNDI tag.
Inside mypropertyfile.properties are some values:
mypropertyfile.properties
mypattern1=AAA
mypattern2=BBB
mypattern3=CCC
However it cannot find ${mypattern1} (or any) and it says it's undefined.
From the debug logs, I can see it is printing:
Setting variable [propertyfile] to [{mypattern1=AAA, mypattern2=BBB, mypattern3=CCC}]
and so I know it is reading the file.
But how can I use the variables within this file? If I were to use ${propertyfile} it would print the whole contents of the file.

log4j appender only sending my messages to stdout, but not remote GELF appender

I'm having a strange issue where log4j is correctly displaying all my log messages the console, but is only sending log messages to my graylog server that I am not writing (e.g. a library I'm using which writes to log.info will show up in graylog, but not any of the messages I write with log.info). What am I missing
Here's my logback-spring.xml
<configuration>
<contextName>test</contextName>
<jmxConfigurator/>
<springProfile name="dev, test">
<appender name="gelf" class="biz.paluch.logging.gelf.logback.GelfLogbackAppender">
<host>udp:localhost</host>
<port>5555</port>
<version>1.1</version>
<extractStackTrace>true</extractStackTrace>
<filterStackTrace>true</filterStackTrace>
<mdcProfiling>true</mdcProfiling>
<timestampPattern>yyyy-MM-dd HH:mm:ss,SSS</timestampPattern>
<maximumMessageSize>8192</maximumMessageSize>
<includeFullMdc>true</includeFullMdc>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
</appender>
</springProfile>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<logger name="com.test" level="info" additivity="false">
<appender-ref ref="stdout" />
</logger>
<root level="INFO">
<appender-ref ref="gelf" />
<appender-ref ref="stdout" />
</root>
</configuration>
I'm instantiating my log by doing:
private static final Logger logger = LogManager.getLogger(MyClass.class);
And writing to my log:
logger.info("WHY CAN'T I SEE THIS IN MY GRAYLOG SERVER");
Edit:
Your are specifying that the loggers of the package com.test should only write to the stdout appender.
<logger name="com.test" level="info" additivity="false">
<appender-ref ref="stdout" />
</logger>
You should change it to:
<logger name="com.test" level="info" />
Make sure that you are running your app with the matching profile for your gelf appender.

Disable the log from specific class/jar via logback.xml

In my application I use Java, Hibernate.
Logging : I use logback.xml
Can anyone suggest if there is a way to disable the logs from the below specific class from Hibernate jar.
LOGGER to be removed from the specific class : ERROR o.h.e.jdbc.spi.SqlExceptionHelper
logback.xml:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<logger name="org.springframework" level="error"
additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Add the following to your logback.xml configuration file:
<logger name="org.hibernate.engine.jdbc.spi.SqlExceptionHelper" level="OFF"/>
The instruction: level="OFF" tells Logback to disable all log output for a given logger.
In your logback.xml configuration adding the following element should work
<logger name="org.hibernate.engine.jdbc.spi.SqlExceptionHelper" level="OFF"/>

Disable Logger in a .jar java library

How to disable logger (print output) of a java library (jar) used in a java program ?
For example i use the jar joda-time-2.8.2.jar
What should I put in my logback.xml file to disable the console output of the jar
logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<Target>System.out</Target>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n</pattern>
</encoder>
</appender>
<logger name="com.paper.white" additivity="false" level="TRACE">
<appender-ref ref="stdout" />
</logger>
<root level="WARN">
<appender-ref ref="stdout" />
</root>
</configuration>
Just create a logger for org.joda.time and set the level to OFF.
<logger name="org.joda.time" level="OFF" >
<appender-ref ref="stdout"/>
</logger>

log4j xml configuration, write some Logger to file and console

I am having following xml config for log4j with 3 appenders. And want to configure logger the following way:
All messages more than INFO are written to STDOUT and global log FILE appenders
But for some class Oauth I want to have extra log file OAUTHFILE
Some classes are filtered and write messages to LOG only on error level this can be achieved by
<logger name="application" level="ERROR" />
some classes are filtered and write messages to its own log file. This is also solved with additivity equal false flag.
<logger name="MapActor" level="DEBUG" additivity="false">
<appender-ref ref="MAPACTORFILE" />
</logger>
But how can I solve the 2nd problem. The folowing configuration produces debug and error messages in STDOUT and! in OAUTHFILE
<configuration>
...
<appender name="STDOUT" ...></appender>
<appender name="FILE" ...>
</appender>
<appender name="OAUTHFILE" ...>
</appender>
...
<logger name="controllers.OAuth" level="DEBUG" additivity="false">
<appender-ref ref="OAUTHFILE" />
</logger>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Actually I have found the solution for second task. I should make 2 STDOUT and FILE appenders - one for general logging and the second one with filtering
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %coloredLevel %cyan(%logger{15}) %message%n%xException{5}</pattern>
</encoder>
</appender>
<appender name="STDOUTERR" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} %coloredLevel %cyan(%logger{15}) %message%n%xException{5}</pattern>
</encoder>
</appender>
And use the second as extra appender-ref for classes that should produce error log to general stdout like here:
<logger name="MapActor" level="DEBUG" additivity="false">
<appender-ref ref="MAPACTORFILEAPPENDER" />
<appender-ref ref="STDOUTERR" />
</logger>

Categories