Can't generate the log file with slf4j? - maven

I have a standard maven project and my maven settings:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>
Under my "resources" directory, I have the two files:
1. boot.properties:
logging.config=classpath:log4j2.xml
log4j2.xml
log
info
error
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %class{36} [%L] [%M] - %msg%xEx%n" />
</Console>
<RollingRandomAccessFile name="INFO-LOG" fileName="${LOG_HOME}/${INFO_FILE_NAME}.log" filePattern="${LOG_HOME}/${INFO_FILE_NAME}_%d{yyyy-MM-dd}_%i.log">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %class{36} [%L] [%M] - %msg%xEx%n" />
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1" />
<SizeBasedTriggeringPolicy size="512000 KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingRandomAccessFile>
<RollingRandomAccessFile name="ERROR-LOG" fileName="${LOG_HOME}/${ERROR_FILE_NAME}.log" filePattern="${LOG_HOME}/${ERROR_FILE_NAME}_%d{yyyy-MM-dd}_%i.log">
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %class{36} [%L] [%M] - %msg%xEx%n" />
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1" />
<SizeBasedTriggeringPolicy size="512000 KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Root level="INFO" includeLocation="true">
<appender-ref ref="CONSOLE" />
<appender-ref ref="INFO-LOG" />
<appender-ref ref="ERROR-LOG" />
</Root>
</Loggers>
My problem is that, when I run my application, I didn't see the "log" directory and log files generated in the root directory.

Slf4J doesn't provide any actual logging implementation.
It looks like you need an slf4j binding for log4j2. slf4j-simple can't read log4j2.xml
From this tutorial start off with the following dependencies
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.7</version>
</dependency>

I think problem is due to access privilege in root folder.
Logger is not able to create a log folder in root directory.
You can check it yourself by trying to create a directory in root folder.
You can users path instead of root directory in log4j2.xml.

Related

Application not writing logs on when log file rotates

My app is using spring log4j2 and uses slf4j api to write log to separate log file "application.log". This app gets deployed to tomcat v8 along with other apps. All app share common log4j2 configuration and writes to common logfile 'application.log'. We have a log rotation policy of 250 mb and when the log file rotates the logs are not written to the logfile surprising only one app among all the app is able to write to the log file. I'm able to reproduce this locally too. Can you please help in fixing this issue.
The other
Please find the log4j2.xml config below.
JAR VERSIONS
slf4j-api 1.7.21
log4j-slf4j-impl 2.5
log4j-api 2.5
log4j-core 2.5
log4j-web 2.5
Log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration name="api-config" status="trace" monitorInterval="30">
<Properties>
<Property name="logdir">/Users/kramesan/microservices-config/logs</Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%X{_requestId} %X{authToken} %X{urlEmployeeId} %X{urlCompanyId} [%X{authEmplIds}] [%X{authCompanyIds}] %d{yyy
y-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</Console>
<RollingFile name="ApplicationLogRollingFile" fileName="${logdir}/application.log"
filePattern="${logdir}/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<JSONLayout locationInfo="true" complete="true" compact="true" eventEol="true" properties="true" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<RollingFile name="AuditLogRollingFile" fileName="${logdir}/audit/api-audit.log"
filePattern="${logdir}/audit/$${date:yyyy-MM}/api-audit-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%X{_requestId} %X{authToken} %X{urlEmployeeId} %X{urlCompanyId} [%X{authEmplIds}] [%X{authCompanyIds}] %m%n</Pattern>
</PatternLayout>
<!-- JSONLayout locationInfo="true" complete="true" compact="true" eventEol="true" properties="true" -->
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<Async name="ApplicationLogAsync" bufferSize="262144">
<AppenderRef ref="ApplicationLogRollingFile"/>
</Async>
<Async name="AuditLogAsync" bufferSize="262144">
<AppenderRef ref="AuditLogRollingFile"/>
</Async>
</Appenders>
<Loggers>
<!-- All the 3rd Party frameworks -->
<Logger name="org.springframework" level="warn" />
<Logger name="org.hibernate" level="warn" />
<!-- common package name for all the business application level code -->
<Logger name="com.trinet" level="info" />
<!-- Audit Loggger This is used for spring aspect to log before and after execution -->
<Logger name="AuditLogger" level="info">
<AppenderRef ref="AuditLogAsync" />
</Logger>
<Root level="info">
<AppenderRef ref="ApplicationLogAsync" />
</Root>
</Loggers>
</Configuration>
Please, edit your Logger name. Logger name property needs package path.
AS-IS
...
<Logger name="AuditLogger" level="info">
...
TO-BE
...
<Logger name="com.foo.bar.AuditLogger" level="info" additivity="false">
...
or
...
<Logger name="com.foo.bar.*" level="info" additivity="false">
...
I think you'd better write property additivity Because your Logger works two times com.foo.bar.AuditLogger Logger and Root Logger. so you additivity to false then it works each.
reference link : Additivity

Can't get file loggin to work with Log4j2 Slf4j in a Java 8 Spring app

I have a java spring 5 application that I deploy to Tomcat 8.5.15. I am now trying to upgrade the logging framework to Log4J2, I also use Slf4J. The problem right now is that I can't seem to log to a file (although it creates a file). It just logs to the console, why?. It's a multi module maven project. In the main "pom" i use these dependencies (for example):
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
<version>1.7.30</version>
</dependency>
Here is the log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="basePath">logs</Property>
</Properties>
<Appenders>
<RollingFile name="fileLogger" fileName="logs/app-info.log" filePattern="logs/app-info-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="foobar" level="debug" additivity="true">
<appender-ref ref="fileLogger" level="debug" />
</Logger>
<Root level="debug" additivity="false">
<appender-ref ref="console" />
</Root>
</Loggers>
</Configuration>
EDIT Here is the 2nd version of the config (This produces a file with a little content, but not from my code.. from testing stuff "DefaultTestContextBootstrapper"...) The log ends up here: mymodule/logs/app-info.log
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<RollingFile name="fileLogger" fileName="logs/app-info.log" filePattern="logs/app-info-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<appender-ref ref="fileLogger" />
</Root>
</Loggers>
</Configuration>
In the java code, I try to log like this:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
..
Logger logger = LoggerFactory.getLogger(MyClass.class);
logger.info("....");
Please edit fileName and filePattern. You wrote absolute path so the log file made at [System
root]/logs/
AS-IS
fileName="logs/app-info.log"
filePattern="logs/app-info-%d{yyyy-MM-dd}.log"
TO-BE
Case 1. Using relative path
If you write relative path then the log will saved at [project root directory]/LOG_FILE_DIR/LOG_FILE_NAME.log
fileName="./LOG_FILE_DIR/LOG_FILE_NAME.log"
filePattern="./LOG_FILE_DIR/LOG_FILE_NAME.log.gz"
Case 2. Using absolute path
If you write absolute path then the log will saved at C:/LOG_FILE_DIR/LOG_FILE_NAME.log
fileName="./LOG_FILE_DIR/LOG_FILE_NAME.log"
filePattern="C:/LOG_FILE_DIR/LOG_FILE_NAME.log.gz"
The problem was that my maven poms were not perfect. I declared the log4j2 in the root pom, but not in the modules, so the jars (log4j2...) were not in the classpath of the final war file, but log4j (1) was there, so I had to get rid of that dependency from a couple of poms. Then I used this config for it to work (log4j2.yml):
configuration:
name: Default
properties:
property:
- name: log-path
value: /logs
- name: archive
value: ${log-path}/archive
appenders:
Console:
PatternLayout:
pattern: '[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n'
name: Console-Appender
target: SYSTEM_OUT
File:
PatternLayout:
pattern: '[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n'
fileName: ${log-path}/amc.log
name: File-Appender
RollingFile:
DefaultRolloverStrategy:
max: '30'
PatternLayout:
pattern: '[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n'
Policies:
SizeBasedTriggeringPolicy:
size: 1 MB
fileName: ${log-path}/rollingfile.log
filePattern: ${archive}/rollingfile.log.%d{yyyy-MM-dd-hh-mm}.gz
name: RollingFile-Appender
loggers:
logger:
additivity: 'false'
appender-ref:
- level: info
ref: Console-Appender
- level: info
ref: File-Appender
- level: info
ref: RollingFile-Appender
level: debug
name: se.su.it.courseservice
root:
appender-ref:
ref: File-Appender
level: info

Why spring boot batch application does not print to console?

I am using sprint boot starter with spring batch. I log everything to log file and console. I use annotation as #Slf4j in class level with lomback.
In the log file everythings is ok but noting comes out of console.
I use perl file which starts the shell script file and the shell file execute the jar file which consist of all my batch application.
My perl file start a shell file and the shell file strats the jar file.
perl:
$command = ". $ENV{'TARI_SOMPROGS'}$bin_pad"."StartAggr.sh $jaa $maan $ENV{'TARI_DBNAAM'} $td 1 1>&2 ";
start_ica_shell($command);
shell file:
java -jar ${TARI_SOFTWARE}/aggre/jar/spring-batch-article-0.0.1-SNAPSHOT.jar $JAA $MAAN $DATABASE $TARI $AGGR_TYPE
Why it does not print to console?
What I tried:
First I used logback-spring.xml with configuration in application.properties as below:
<configuration scan="true" debug="true">
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="guru.springframework.controllers" level="WARN" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
<logger name="guru.springframework.helpers" level="WARN" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
and application.properties
spring.application.name=aggregation
spring.main.banner-mode=log
debug=true
spring.batch.job.enabled=false
spring.output.ansi.enabled=detect
logging.level.root=WARN
logging.level.com.ava.batch=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.sql=DEBUG
logging.level.org.hibernate=ERROR
logging.file.max-size = 11
logging.pattern.console= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
logging.file =/ava/tar/print/log/aggregatie/${spring.application.name}.log
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
and my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.toptal</groupId>
<artifactId>spring-batch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-batch-article</name>
<description>Demo project for Spring Batch</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter</artifactId>-->
<!--<exclusions>-->
<!--<exclusion>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-logging</artifactId>-->
<!--</exclusion>-->
<!--</exclusions>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-cli -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-cli</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<!--Logback Begin-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!--Logback End-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.3.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
Secondly I used logback.xml as below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="30 seconds">
<property name="LOG_PATH" value="/ava/tari/print/log/aggregatie" />
<property name="LOG_ARCHIVE" value="${LOG_PATH}/archive" />
<timestamp key="timestamp-by-second" datePattern="yyyyMMdd'T'HHmmss"/>
<appender name="Console-Appender" class="ch.qos.logback.core.ConsoleAppender">
<layout>
<pattern>%msg%n</pattern>
<!--<pattern>%d{yyyy/MM/dd uu:mm:ss, SSS} [%thread] %-5level %logger{36} -%m %throwable{0} %n</pattern>-->
</layout>j
</appender>
<appender name="File-Appender" class="ch.qos.logback.core.FileAppender">
<file>${LOG_PATH}/logfile-${timestamp-by-second}.log</file>
<encoder>
<!--<pattern>%msg%n</pattern>-->
<pattern>%d{yyyy/MM/dd uu:mm:ss, SSS} [%thread] %-5level %logger{36} -%m %throwable{0} %n</pattern>
<outputPatternAsHeader>true</outputPatternAsHeader>
</encoder>
</appender>
<appender name="RollingFile-Appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/rollingfile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_ARCHIVE}/rollingfile.log%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>1KB</totalSizeCap>
</rollingPolicy>
<encoder>
<!--<pattern>%msg%n</pattern>-->
<pattern>%d{yyyy/MM/dd uu:mm:ss, SSS} [%thread] %-5level %logger{36} -%m %throwable{0} %n</pattern>
</encoder>
</appender>
<appender name="Async-Appender" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="RollingFile-Appender" />
</appender>
<logger name="be.kava.batch.aggregation" level="info" additivity="false">
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-Appender" />
<appender-ref ref="Async-Appender" />
</logger>
<root>
<appender-ref ref="Console-Appender" />
</root>
</configuration>
I added select STDOUT;in the perl file before starting mijn shell command as below.
This solved the problem of redirection of output back to normal.
select STDOUT;
$command = ". $ENV{'TARI_SOMPROGS'}$bin_pad"."StartAggr.sh $jaa $maan $ENV{'TARI_DBNAAM'} $td 1 1>&2 ";
start_ica_shell($command);

Spring 5 Reactor Netty log spam (too verbose)

Is there a way to reduce console/log noise output in Spring 5 + Reactor + Netty. It's too verbose, especially full print out of a request:
This is what helped me:
pom.xml
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
src/main/resources/logback.xml
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.sssXXX, UTC} [%thread] %-5level %logger{15} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>

No extra logging in the console with log4j

I'm new at this logging stuff and i want see logging from spring to see all the beans created.
So I want to try logging with log4j but no extra loggin appear in the console.
I follow some example to make my logging.
Here's my configuration :
pom.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0-rc2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0-rc2</version>
</dependency>
log4j.properies
# Root logger option
log4j.rootLogger=INFO, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.logger.org.springframework=INFO,stdout
My Controller Class
//Import log4j classes.
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
#Transactional
#Controller
public class Inscription {
...
static final Logger logger = LogManager.getLogger(Inscription.class);
...
#RequestMapping(value="/")
public String Test(ModelMap model) {
...
//log it via log4j
logger.debug(model);
...
}
}
is the log4j.property loaded ? (i put it twice to make sure)
what mean this logger.debug(model); is it requierd to make logging or just log4j.property is enough ?
I have created a log4j2.xml in classpath (src folder) it will detect automatically by log4j
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="CONSOLE"/>
</Root>
<Logger name="controller" level="ALL" >
<AppenderRef ref="CONSOLE"/>
</Logger>
</Loggers>
</Configuration>

Resources