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
Having trouble getting sentry issues to publish.
Here's my setup.
I have exported my DSN as SENTRY_DSN.
src/main/kotlin/resources/log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn" packages="org.apache.logging.log4j.core,io.sentry.log4j2">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<Sentry name="Sentry" />
</appenders>
<loggers>
<root level="INFO">
<appender-ref ref="Console" />
<!-- Note that the Sentry logging threshold is overridden to the WARN level -->
<appender-ref ref="Sentry" level="WARN" />
</root>
</loggers>
</configuration>
build.gradle.kts
...
dependencies {
compile("io.sentry:sentry-log4j2:1.7.22")
...
}
Controller.kt
#RestController
#RequestMapping("/users")
class AuthenticationController(private val exampleService: ExampleService) {
private val logger = LogManager.getLogger(AuthenticationService::class)
#DeleteMapping("/session")
fun logout(): Mono<Response> {
logger.error("this is a error")
return exampleService.returnMono()
}
}
I expect this logger.error call to send a message to sentry.
Sentry SDK for Java recently got support for Webflux:
https://github.com/getsentry/sentry-java/pull/1529
That was on version 5.1.0-beta3 and GA is about to land in a few days/weeks:
https://github.com/getsentry/sentry-java/blob/main/CHANGELOG.md#510-beta3
I currently use spring cloud netflix with log4j2. The log4j2 configuration comes from the xml in the classpath. When I run the app, I see that the feign & ribbon logs are not being redirected to the logger specified in the configuration. I have configured log for com.netflix.ribbon & feign packages to be logged at debug level.
However, log configured for spring is properly redirecting to the specified appender, ribbon & feign are not.
I am using gradle with spring-boot-starter-logging ignored & added spring-boot-starter-log4j2 in as part of my build.
I see that feign has a way by which we can configure slf4j, but since we use annotation driven feign support, I cant configure the feign to use slf4j for logging.
Any help is appreciated.
My log4j2.xml looks some what like
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">logs</Property>
<Property name="log-fileName">test</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
<RollingFile name="trace-log" fileName="${log-path}/${log-fileName}-trace.log" filePattern="${log-path}/${log-fileName}_trace-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/${log-fileName}-error.log" filePattern="${log-path}/${log-fileName}_error-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<logger name="org.springframework" level="trace" additivity="false">
<AppenderRef ref="trace-log" />
</logger>
<logger name="feign" level="trace" additivity="false">
<AppenderRef ref="trace-log" />
</logger>
<logger name="com.netflix.ribbon" level="trace" additivity="false">
<AppenderRef ref="trace-log" />
</logger>
<Root level="info">
<AppenderRef ref="console-log"></AppenderRef>
<AppenderRef ref="error-log" level="ERROR"/>
</Root>
</Loggers>
</Configuration>
PS: The reason for debugging feign/ribbon is to understand a weird feign behavior between two different machines in our micro services setup
Looking at Spring Cloud's FeignClientFactoryBean shows that you can optionally autowire a bean of type feign.Logger.Level. Try registering such a bean in your #Configurationusing
#Bean
public feign.Logger.Level feignLoggerLevel() {
return feign.Logger.Level.FULL;
}
#jensfischerhh's answer would fix many cases but looks mistakenly missed one thig.
You need to config feign generated class's logger level with feignLoggerLevel Bean.
Both config must be exist together.
related doccument in spring-cloud-netflix
bean config ( in #Configuration annotated class )
#Bean
public feign.Logger.Level feignLoggerLevel() {
return feign.Logger.Level.FULL;
}
log config
</Configuration>
<Loggers>
<logger name="your.feign-interface-package" level="trace">
</Loggers>
</Configuration>
I have a problem with configuration on Logback in a Spring Boot application. I want my consoleAppender to look like the default Spring Boot console appender. How to inherit pattern from Spring Boot default console appender?
Below is my consoleAppender configuration
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern class="org.">
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
Once you have included the default configuration, you can use its values in your own logback-spring.xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<!-- use Spring default values -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
…
</configuration>
You can find Spring Boot logback console logging pattern in defaults.xml file:
spring-boot-1.5.0.RELEASE.jar/org/springframework/boot/logging/logback/defaults.xml
Console pattern:
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx
</Pattern>
</layout>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
If you are using application.yml for your config, you can set the logging pattern this way:
logging:
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss} | %-5level | %logger{1.} | %msg%n"
level:
org.springframework: WARN
com.ulisesbocchio.jasyptspringboot: WARN
com.example.test: DEBUG
You can override the logging level on the command line. For example:
$ java -Dlogging.level.com.example.test=TRACE -jar my-example.jar
It's been some time since this question was asked but since I had the problem myself recently and couldn't find an answer I started digging a bit deeper and found a solution that worked for me.
I ended up using the debugger and take a look at the default appenders attached to the logger.
I found this pattern to be working as desired for me:
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p 18737 --- [%t] %-40.40logger{39} : %m%n%wEx</pattern>
EDIT: The pattern is not entirely correct, I saw that runtime some values had already been instantiated (in this case 18737 ---) i will look into the proper variable to substitute there. It does contain the format for fixed length columns though
EDIT 2: Ok, I took another look at the debugger contents. This you can also do yourself by looking at the contents of a logger instance:
Debugger(eclipse) Logger Contents
So I ended up using the pattern used in the consoleAppender:
%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(18971){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx
As can be seen here:
Debugger: detailed contents of the encoder pattern
Logging pattern can be configured using application.properties file
Example :
# Logging pattern for the console
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
You can use below pattern :
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%15.15t] %-40.40logger{1.} : %m%n%wEx
Note that you can also customize the imported properties.
But beware that at least with spring boot 1.4.3 if you want to customize the properties imported from the defaults.xml, then the customization should be placed BEFORE the include.
For example this customizes the priority to 100 character wide:
<configuration scan="true">
<property name="LOG_LEVEL_PATTERN" value="%100p" />
<!-- use Spring default values -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
</layout>
</appender>
<logger name="hu" level="debug" additivity="false">
<appender-ref ref="CONSOLE" />
</logger>
<root level="warn">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
But this is NOT:
<configuration scan="true">
<!-- use Spring default values -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="LOG_LEVEL_PATTERN" value="%100p" />
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
</layout>
</appender>
<logger name="hu" level="debug" additivity="false">
<appender-ref ref="CONSOLE" />
</logger>
<root level="warn">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
For those who'd like to use Łukasz Frankowski's answer (which looks like the cleanest solution here), but in a groovy version, the "problematic" {$PID:- } part can be expanded like in the following:
logback-spring.groovy
import ch.qos.logback.classic.PatternLayout
import ch.qos.logback.core.ConsoleAppender
import org.springframework.boot.logging.logback.ColorConverter
import org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter
import org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter
import static ch.qos.logback.classic.Level.INFO
conversionRule("clr", ColorConverter)
conversionRule("wex", WhitespaceThrowableProxyConverter)
conversionRule("wEx", ExtendedWhitespaceThrowableProxyConverter)
appender("STDOUT", ConsoleAppender) {
layout(PatternLayout) {
def PID = System.getProperty("PID") ?: ''
pattern = "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx"
}
}
root(INFO, ["STDOUT"])
This worked for me, adding following line to resources/log4j2.properties file
appender.console.layout.pattern = %d{ISO8601} - info: %msg%n ( your custom pattern goes here )
The spring documentation has an example of the logback.xml that defines the default.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
<logger name="org.springframework.web" level="DEBUG"/>
</configuration>
I have Log4j2 configuration:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p - %m%n"/>
</Console>
<Flume name="flume" >
<MarkerFilter marker="FLUME" onMatch="ACCEPT" onMismatch="DENY"/>
<Agent host="IP_HERE" port="6999"/>
</Flume>
<File name="file" fileName="flume.log">
<MarkerFilter marker="FLUME" onMatch="ACCEPT" onMismatch="DENY"/>
</File>
</appenders>
<loggers>
<root level="info">
<appender-ref ref="file"/>
<appender-ref ref="console"/>
<appender-ref ref="flume"/>
</root>
</loggers>
</configuration>
And the Flume agent:
agent1.channels = ch1
agent1.sources = avro-source1
agent1.sinks = hdfs-sink1
agent1.channels.ch1.type = memory
agent1.sources.avro-source1.type = avro
agent1.sources.avro-source1.bind = IP_HERE
agent1.sources.avro-source1.port = 6999
agent1.sinks.hdfs-sink1.type = hdfs
agent1.sinks.hdfs-sink1.hdfs.path = hdfs://hadoop/user/hduser/bond/flume
agent1.sinks.hdfs-sink1.hdfs.fileType = DataStream
agent1.sinks.hdfs-sink1.hdfs.writeFormat = Text
agent1.sinks.hdfs-sink1.channel = ch1
agent1.sources.avro-source1.channels = ch1
When I send a log,first time I get on my web server console an error: "ERROR Recursive call to appender flume",(Flume Agent is active and it has made bind process correctly) on next loggers events theres no error and a file is written on hadoop, But when I try to visualise it in Text mode with HUE, I can't se the text I have sent, I only see binary codes...
Is there any mistake? or what I have to do so I can write the file in text mode?
Thank you
As easy as:
Instead of:
<Flume name="flume" >
Is:
<Flume name="flume" compress="false" type="Avro">
Thanks to a good example