Why Logger not logged?? - java

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.

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

Logback and different levels

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.

log4j not writing messages to file

Looked at quite a few examples on this and have seen some confusing stuff. Here is the log4j.xml file
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="1000KB" />
<param name="maxBackupIndex" value="5" />
<!-- For Tomcat -->
<param name="file" value="${catalina.home}/logs/demandManage.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="com.xxx.demand.domain.data" additivity="true">
<level value="debug" />
<appender-ref ref="file"/>
</logger>
<logger name="com.xxx.rita.serviceserver.controller.AbstractRestController" additivity="true">
<level value="debug" />
<appender-ref ref="file"/>
<appender-ref ref="console" />
</logger>
<logger name="com.xxx.dao.AbstractDAOImpl" additivity="true">
<level value="debug" />
<appender-ref ref="file"/>
<appender-ref ref="console" />
</logger>
<logger name="org.hibernate" >
<level value="warn" />
<appender-ref ref="file"/>
</logger>
<root>
<priority value ="warn" />
<appender-ref ref="console" />
<appender-ref ref="file"/>
</root>
</log4j:configuration>
In my java class:
package com.xxx.dao;
public abstract class AbstractDAOImpl<T, ID extends Serializable> implements AbstractDAO<T,ID>{
private Logger logger = LoggerFactory.getLogger(com.xxx.dao.AbstractDAOImpl.class);
#SuppressWarnings("unchecked")
public T read(ID id){
T response = (T) getCurrentSession().get(clazz, id);
if(response==null){
logger.error(RiskTaker.name.get()+ " tried to load " + clazz + " of " +id);
throw new RuntimeException("hibernate object did not exist");
}
return response;
}
}
So when I am hitting the logger.error message with this configuration, nothing is going to the demandManage.log file or the console. I don't see what's wrong. When I had root set to debug, hibernate of course practically fills up that log file so it does work.
Is there a difference between using level vs priority? Like a slfj4 vs appache logger? The root sets priority but the child loggers are using level.
I am starting tomcat in debug mode and know I am hitting those logger.error statements but the file remains empty. May be I need to flush to it right away?
I previously had the logger in the xml file as fully qualified and in the java class as just class name only but fixing that did nothing. I am wondering what I am missing. Thank you in advance.

Put Local Machine Name Into mySQL Table

I have a log table in my database that keeps track of login, logout, timeout, and what the user does while they are logged on. My Java is set up so that every time I do LOGGER.info(message), the message goes into the MESSAGE column of my table.
Here is the log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<appender name="db" class="org.apache.log4j.jdbcplus.JDBCAppender">
<param name="connector" value="com.blahblahblah.jdbcplus.MySqlConnectionHandler" />
<param name="sql"
value="INSERT INTO LOG(LEVEL, MESSAGE, TIMESTAMP, HOST) VALUES ('#PRIO#', '#MSG#', '#TIMESTAMP#', 'Test')" />
<layout class="org.apache.log4j.PatternLayout"></layout>
</appender>
<!-- Application Loggers -->
<logger name="com.blahblahblah.service.ZebraService">
<level value="info" />
<appender-ref ref="db" />
</logger>
<logger name="com.blahblahblah.service.ZplTemplateService">
<level value="info" />
<appender-ref ref="db" />
</logger>
<logger name="com.blahblahblah.service.MyService">
<level value="info" />
<appender-ref ref="db" />
</logger>
<logger name="com.blahblahblah.web.controller.ViewController">
<level value="info" />
<appender-ref ref="db" />
</logger>
<logger name="com.blahblahblah.security">
<level value="info" />
<appender-ref ref="db" />
</logger>
<!-- 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.springframework.web.servlet.DispatcherServlet">
<level value="error" />
</logger>
<logger name="org.springframework.security">
<level value="error" />
</logger>
<logger name="org.springframework.aop">
<level value="error" />
</logger>
<logger name="org.springframework.ui">
<level value="error" />
</logger>
<!-- Root Logger -->
<root >
<priority value="error" />
<appender-ref ref="console" />
</root>
This prints "INFO" in the LEVEL column, my information about login/logout in the MESSAGE column, the timestamp in the TIMESTAMP column, and "Test" in the HOST column.
I want to have the HOST column be the local machine name. For example, my computer's name is PC-1050, so every time I do something on that computer, I want HOST to say "PC-1050".
I have no idea how to do the #MSG#, #TIMESTAMP#, etc. I was just following what I was told to do. Can anyone offer some assistance? Thanks.
I did some research on log4j configuration for JDBCAppender and unfortunately did not see any easy way to do it (i.e. nothing along the lines of a simple change to the log4j.xml file) for Hostname.
I did not find anything for Hostname along the lines of
Priority #PRIO#
Message #MSG#
Timestamp #TIMESTAMP#
for your SQL of
INSERT INTO LOG(LEVEL, MESSAGE, TIMESTAMP, HOST) VALUES
('#PRIO#', '#MSG#', '#TIMESTAMP#', 'Test')
Also, I did not see anything in log4j documentation on PatternLayout for Hostname. I woudl think that if there was a macro for it in log4j JDBCAppender, it would correspond to a conversion character in PatternLayout.
You could look at putting the Hostname into the MDC. Here is an example of someone else who uses log4j JDBCAppender and put the user into the MDC.
Good luck, hope this helps!

Categories