Logback and different levels - java

There 3 appenders: STDOUT, FILE-AUDIT and FILE-ERROR. How to log all messages to STDOUT and FILE_AUDIT(including errors), but errors messages dublicate in FILE-ERROR. Part of my logback.xml:
<logger level="all"
additivity="true">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</logger>
<root level="error">
<appender-ref ref="FILE-ERROR" />
</root>

You should define your FILE-ERROR appender with a ThresholdFilter:
<appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>[...]</file>
<!-- [...] -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
</appender>
Then, simply add all your appenders to root logger:
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="FILE-ERROR" />
</root>
This way, STDOUT and FILE-AUDIT contain all the messages, but FILE-ERROR only contains error messages.

Related

How to organize daily logging files in Logback

I am using Logback for logging. Now all logs are writing into one file, and it becomes very large. How to organize logging in such a way that a new file is created at the beginning of a new day?
I use such logback.xml
<configuration>
<property name="LOG_PATTERN" value="%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n" />
<property name="APP_LOG_ROOT" value="admin-logs"/>
<timestamp key="year" datePattern="yyyy" />
<timestamp key="month" datePattern="MM" />
<timestamp key="date" datePattern="dd" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-5relative %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger level="INFO" name="my.name">
<appender-ref ref="STDOUT" />
</logger>
<root level="ERROR">
<appender-ref ref="STDOUT" />
</root>
</configuration>
all logs are writing into one file
The shown config is going to the STDOUT, not a file.
If you want to write to a daily file, use logrotate/cron process, or have logback do this itself.
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_DIR}/bim.%d{yyyy-MM-dd}.log</fileNamePattern>
...
</appender>
...
<logger level="INFO" name="my.name">
<appender-ref ref="file" />
</logger>
<root level="ERROR">
<appender-ref ref="file" />
</root>

How can i avoid the queries of my spring-boot with hibernate app will be written to console or file?

I have an app written in spring-boot.
I have the following files:
application.properties
spring.main.banner-mode=off
debug=false
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.format_sql=false
spring.jpa.properties.hibernate.use_sql_comments=false
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProfile name="dev">
<property name="LOGS" value="...." />
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</layout>
</appender>
<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/signing-dev.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGS}/archived/signing-dev-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</root>
<logger name="[mypackage]" level="error" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>
<logger name="level.com.zaxxer" level="error" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>
<logger name="org.hibernate" level="error" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>
<logger name="org.thymeleaf" level="error" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>
<logger name="org.springframework.security" level="error" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>
</springProfile>
</configuration>
I want to avoid that my hibernate queries will be written to console or file, but they are written like this:
Hibernate: select employeead0_.id as id1_0_0_, employeead0_.type as type2_0_0_ from employee_admin employeead0_ where employeead0_.id=?
How can i configure logback to avoid this?
try add this to application.properties:
#show sql statement
logging.level.org.hibernate.SQL=error
#show sql values
logging.level.org.hibernate.type.descriptor.sql=error

How to turn off Soap Message CXF form STDOUT using logback

I'm working on web service using Apache cxf but in my console and catalina.out I had a lot of extra logs ,I'm using logback for logging.
I want to turn off logging soap message generated by org.apache.cxf.services,this my logback configuration :
<!-- Standard Output logger -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<!-- File Appender -->
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/logs/output-log.log</file>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>1MB</maxFileSize>
</triggeringPolicy>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/logs/output-log-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} %-5p [%-10.10t]
%60.60c:%-3L%m%n
</pattern>
</encoder>
</appender>
<logger name="org.apache.cxf" level="ERROR" additivity="false">
<appender-ref ref="AUDITTRAIL" />
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
<!-- appender-ref ref="SYSLOG" / -->
</logger>
<logger name="org.apache.cxf.interceptor" level="ERROR"
additivity="false" />
<logger name="org.apache.cxf.services" level="ERROR" additivity="false" />
<root level="INFO">
<appender-ref ref="AUDITTRAIL" />
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
<!-- appender-ref ref="SYSLOG" / -->
</root>
Using this configuration I still get soap message in my console log and outlog file example :
mars 30, 2018 9:55:00 AM org.apache.cxf.services.WebServiceImplService.StartImplPort.WebService
INFOS: Inbound Message
----------------------------
ID: 5
Address: http://127.0.0.1:8089/glsid/ws/webService
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=utf-8
Headers: {Accept=[text/xml, text/html, image/gif, image/jpeg,
.......
Any help to fix this problem thanks in advance
Finally this solution work for me :
in the file : META-INF/cxf/org.apache.cxf.Logger
I added : org.apache.cxf.common.logging.Slf4jLogger
In the spring configuration : applicationContext :
I added :
<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<cxf:bus>
<cxf:inInterceptors>
<ref bean="loggingInInterceptor"/>
</cxf:inInterceptors>
<cxf:outInterceptors>
<ref bean="loggingOutInterceptor"/>
</cxf:outInterceptors>
</cxf:bus>
and to turn off soap logging in STDOUT in the logback.xml :
<!-- Apache CXF logger -->
<logger name="org.apache.cxf" level="INFO"/>
<logger name="org.apache.cxf.interceptor" additivity="false">
<!-- <appender-ref ref="STDOUT" /> -->
<appender-ref ref="FILE" />
</logger>
<root level="INFO">
<!-- <appender-ref ref="STDOUT" />-->
<appender-ref ref="FILE" />
</root>

Why Logger not logged??

This is my Log4j config file:
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="error" />
</logger>
<logger name="org.springframework.beans">
<level value="error" />
</logger>
<logger name="org.springframework.context">
<level value="error" />
</logger>
<logger name="org.springframework.web">
<level value="error" />
</logger>
<logger name="org.hibernate">
<level value="error" />
</logger>
<logger name="org.springframework.security">
<level value="error" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
.
And I use a logge into a service for example, like this:
private static final Logger LOG = LoggerFactory.getLogger(CommessaServiceImpl.class);
and then:
LOG.info("Start method");
But I can see nothing about it into my console...
Why??
Because you are logging info(LOG.info) and logging level is set to error and warn.
Change it to
<root>
<priority value="info" />
<appender-ref ref="console" />
</root>
There are a couple of reasons you will not see logs.
You have not defined any appenders. You need something like:
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<param name="ConversionPattern" value="%d{dd-MM-yy HH:mm:ss,SS} %5p [%t] (%F:%L) - %m%n" />
</layout>
</appender>
Your log levels are all set to "error" and you are trying to log at the "info" level.

Glassfish and log4j

I am trying to deploy 2 servlets in glassfish with log4j library.
I have no problems in tomcat. Each servlet writes in its own file but in tomcat, I found this guide https://blogs.oracle.com/naman/entry/configure_log4j_for_use_in to configure and support log4f library.
I got the first servlet working with it. And it writes logs in its log file. But whenever I deploy the secod servlet, I don't see its logs. The first one keeps writing in its own file but the second one not.
I modify the logback.xml file included in config folder to include both files and all packages but it seems to ignore it. This is my logback.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.lordofthejars.foo" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<appender name="SERVER-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->0
<fileNamePattern>/root/glassfish4/glassfish/domains/domain1/logs/serverregister-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- keep 7 days' worth of history -->
<maxHistory>7</maxHistory>
<TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>512MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<append>true</append>
<encoder>
<pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%p] %logger{5}:%L - %msg%n</pattern>
</encoder>
</appender>
<appender name="REGMAN-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<!--<fileNamePattern>${catalina.home}/logs/registermanager-%d{yyyy-MM-dd}.%i.log</fileNamePattern>-->
<fileNamePattern>/root/glassfish4/glassfish/domains/domain1/logs/registermanager-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- keep 7 days' worth of history -->
<maxHistory>7</maxHistory>
<TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>512MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<append>true</append>
<encoder>
<pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%p] %logger{5}:%L - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.eu.iotapi.handlers.AuthenticationGatewayHandler" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.handlers.AuthenticationHandler" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.handlers.DeviceValidationHandler" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.handlers.EventValidationHandler" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.handlers.GatewayValidationHandler" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.handlers.ObservationValidationHandler" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.resources.PublicationsResource" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.resources.RegisterDeviceOnBackground" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.resources.RegisterGatewayOnBackground" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.resources.RegistrationResource" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.resources.SubmitEventOnBackground" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.resources.SubmitObservationOnBackground" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.uns.device.RegisterDeviceHandler" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.uns.events.SubmitEventsHandler" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.uns.device.RegisterGatewayHandler" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.uns.observations.SubmitObservationsHandler" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.iotapi.resources.DeviceTranslator" level="DEBUG" additivity="false">
<appender-ref ref="SERVER-FILE"/>
</logger>
<logger name="com.eu.registermanager.eventbroker.device.registration.DeviceRegistrationHandler" level="DEBUG" additivity="false">
<appender-ref ref="REGMAN-FILE"/>
</logger>
<logger name="com.eu.registermanager.eventbroker.gateway.GatewayRegistrationHandler" level="DEBUG" additivity="false">
<appender-ref ref="REGMAN-FILE"/>
</logger>
<logger name="com.eu.registermanager.handlers.AuthenticationGatewayHandler" level="DEBUG" additivity="false">
<appender-ref ref="REGMAN-FILE"/>
</logger>
<logger name="com.eu.registermanager.handlers.AuthenticationHandler" level="DEBUG" additivity="false">
<appender-ref ref="REGMAN-FILE"/>
</logger>
<logger name="com.eu.registermanager.resources.RegisterDeviceOnEventBrokerOnBackground" level="DEBUG" additivity="false">
<appender-ref ref="REGMAN-FILE"/>
</logger>
<logger name="com.eu.registermanager.resources.RegisterGatewayOnEventBrokerOnBackground" level="DEBUG" additivity="false">
<appender-ref ref="REGMAN-FILE"/>
</logger>
<logger name="com.eu.registermanager.resources.RegisterResource" level="DEBUG" additivity="false">
<appender-ref ref="REGMAN-FILE"/>
</logger>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

Categories