I would like to use the following Conversion Pattern
%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1.}:%L - %m%n
Which produces output like
2016-06-08 10:29:40 [http-nio-8080-exec-8] DEBUG h.d.h.l.l.s.w.f.MyClass:27 - This is a debug message.
2016-06-08 10:29:40 [http-nio-8080-exec-8] INFO h.d.h.l.l.s.w.f.MyClass:22 - This is an info message.
2016-06-08 10:29:40 [http-nio-8080-exec-8] WARN h.d.h.l.l.s.w.f.MyClass:33 - This is a warn message.
2016-06-08 10:29:40 [http-nio-8080-exec-8] ERROR h.d.h.l.l.s.w.f.MyClass:39 - This is an error message.
2016-06-08 10:29:40 [http-nio-8080-exec-8] FATAL h.d.h.l.l.s.w.f.MyClass:45 - This is a fatal message.
However when I run my tests and trigger my log4j file I get the error message
log4j:ERROR Category option "1." not a decimal integer.
Log4j and slf4j are setup in my pom with
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.19</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.19</version>
<scope>runtime</scope>
</dependency>
What version of log4j do I need to get 1. to be a valid Category option.
I was using PatternLayout not EnhancedPatternLayout
%c{1.}
Is only available in EnhancedPatternLayout
Related
For some reason, my log4j configuration file is getting ignored.
The configuration is file found in
C:\dev\pa-backup-restore-utility\BackupRestore\log4j2.properties
Here are the contents of the file:
name=PropertiesConfig
property.filename = logs
appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=logs/backupRestore.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
loggers=file
logger.file.name=log4j2.properties
logger.file.level = error
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = error
rootLogger.appenderRefs=stdout,logfile
rootLogger.appenderRef.stdout.ref=STDOUT
rootLogger.appenderRef.logfile.ref=LOGFILE
The batch file that runs the class is located in the same directory as the configuration file.
The command from the batch file is run as follows:
set javaPath="%appDir%\jre\bin\java.exe"
%javaPath% -Dapp.properties.path=windows.app.properties -Dlog4j.configurationFile=file:log4j2.properties -jar backupRestore-jar-with-dependencies.jar -b
The entry in pom.xml for this is:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.16.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>
Here's how a logger.info is called:
static void printWarning(String message) {
logger.info("============== warning ===============================");
logger.info("== " + message);
logger.info("============== warning ===============================");
}
However, when I run the batch job, an info message gets displayed in the console:
[main] INFO com.sick.backup.util.Utils - ============== warning ===============================
[main] INFO com.sick.backup.util.Utils - == here is an example message
[main] INFO com.sick.backup.util.Utils - ============== warning ===============================
Also, a log file is no longer getting generated.
This was all working at what point. I'm trying to track down the version to make sure it matches, but the only change I've made is to upgrade the version of log4j-to-slf4j due to prevent a vulnerability.
I am creating a Maven project for Selenium in eclipse. Don't know why it threw log4j error (It didn't used to earlier, before upgrading Eclipse). The error is as follows -
I have already added "log4j.properties" file under src/main/resources as -
log4j.rootLogger=INFO, 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
Also added dependency as following in POM.xml -
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.26</version>
</dependency>
Artifacts used -
Eclipse - Version: 2020-06 (4.16.0)
Maven artifact id - maven-archetype-quickstart - v1.4
Selenium version - 3.141.59
The error is not related to log4j. The error is for org.reactivestreams.Publisher. Add the following Maven dependency to get it:
<!-- https://mvnrepository.com/artifact/org.reactivestreams/reactive-streams -->
<dependency>
<groupId>org.reactivestreams</groupId>
<artifactId>reactive-streams</artifactId>
<version>1.0.3</version>
</dependency>
Make sure to update the project after adding the dependency.
While creating RollingFileAppender with a System property variable I got the following error.
ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile. java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:132)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:918)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:858)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:850)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:479)
at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:219)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:231)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:496)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:566)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:582)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:217)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:551)
at com.hifx.lens.services.AvroSerializerFactory.<init>(AvroSerializerFactory.java:16)
at com.hifx.lens.services.AvroSerializerFactory.init(AvroSerializerFactory.java:43)
at com.hifx.lens.Accumulo.main(Accumulo.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassCastException: org.apache.logging.log4j.core.appender.FileManager cannot be cast to org.apache.logging.log4j.core.appender.rolling.RollingFileManager
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.getFileManager(RollingFileManager.java:103)
at org.apache.logging.log4j.core.appender.RollingFileAppender.createAppender(RollingFileAppender.java:191)
... 27 more
My log4j2.yml configuration file is
Configutation:
name: Default
Properties:
Property:
name: LOG_PATH
value: "logs"
Appenders:
Console:
name: Console_Appender
target: SYSTEM_OUT
PatternLayout:
pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
File:
name: File_Appender
fileName: ${sys:LOG_PATH}/log.log
PatternLayout:
pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
RollingFile:
name: RollingFile_Appender
fileName: ${sys:LOG_PATH}/log.log
filePattern: "logs/archive/rollingfile.log.%d{yyyy-MM-dd-hh-mm}.gz"
PatternLayout:
pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
Policies:
SizeBasedTriggeringPolicy:
size: 100 KB
DefaultRollOverStrategy:
max: 30
Loggers:
Root:
level: debug
AppenderRef:
- ref: RollingFile_Appender
Logger:
- name: Accumulo_File_logger
level: debug
AppenderRef:
- ref: File_Appender
If I change the filename of RollingFile_Appender to using property name ie:
fileName: ${LOG_PATH}/log.log
then the error is disappeared and everything working fine.
If I use the File_Appender( which is also using the same System property variable(sys:LOG_PATH)) then also everything working fine.
Same error if I change sys: to env:
I think there is some parsing issue with the jackson.
I need to configure the log path from environment variables.
Dependencies using are
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.6.2</version>
</dependency>
Somebody please help me
This is because you defined both File_Appender and RollingFile_Appender to log into the same file. They kind of "own" their log files, so once a File appender has been created for the ${sys:LOG_PATH}/log.log you cannot create a RollingFile appender pointing to the same file. The second appender in the configuration file tries to create a LogManager that would be responsible for the file, finds the first one being already there so tries to cast into the class that it needs - and fails.
If you move the RollingFile_Appender above File_Appender the exception will be:
main ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.FileAppender for element File. java.lang.reflect.InvocationTargetException
Now the RollingFile_Appender would own the ${sys:LOG_PATH}/log.log file and you wouldn't be able to create the File_Appender because of the same reason.
Solution: remove one of the appenders, having both at the same time doesn't make sense anyway - they would have to compete with each other, or point each of the to a different file.
my code is working fine if i right click on server and run as run on server , but when i try to deploy it on tomcat and try to run it it is giving me below error
log4j.properties file:
log4j.logger.Controller = INFO,error,stdout
log4j.logger.Client = INFO,error,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
error:
log4j:ERROR Could not find value for key log4j.appender.error
log4j:ERROR Could not instantiate appender named "error".
log4j:ERROR Could not find value for key log4j.appender.error
log4j:ERROR Could not instantiate appender named "error".
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
pom.xml :
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
The problem is in these two lines:
log4j.logger.Controller = INFO,error,stdout
log4j.logger.Client = INFO,error,stdout
You can only specify one logger level as the first argument (in this case INFO), and then the next two arguments are going to be considered the appender names to use for that logger (in this case error and stdout). Because no appender named error exists, you are getting the errors you've reported.
Check this out (https://logging.apache.org/log4j/1.2/manual.html) that shows an example of assigning a logger to two different appenders that looks a lot like your code. Hopefully this will help explain why log4j is looking for an appender named error in your application.
Here is another configuration file that uses multiple appenders.
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
Mongo java driver is verbose since v3.x.x, it's not very optimal to store a log of logs like this. Would like to hide some of them.
Here are my dependencies (maven / pom.xml)
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.1</version>
</dependency>
<!-- LOGS -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
I want to hide mongo java driver logs :
2015-05-16 10:43:22 023 DEBUG command:56 - Sending command {count : BsonString{value='skydatas'}} to database rizze on connection [connectionId{localValue:2, serverValue:9}] to server 127.0.0.1:27017
2015-05-16 10:43:22 041 DEBUG command:56 - Command execution completed
2015-05-16 10:43:22 042 DEBUG command:56 - Sending command {count : BsonString{value='skydatas'}} to database rizze on connection [connectionId{localValue:2, serverValue:9}] to server 127.0.0.1:27017
2015-05-16 10:43:22 060 DEBUG command:56 - Command execution completed
2015-05-16T10:49:34.448Z DEBUG Checking status of 127.0.0.1:27017 | | cluster:56
2015-05-16T10:49:34.452Z DEBUG Updating cluster description to {type=STANDALONE, servers=[{address=127.0.0.1:27017, type=STANDALONE, roundTripTime=1,3 ms, state=CONNECTED}] | | cluster:56
Thanks
You should be able to do this from your log4j configuration file.
The first thing I would do is temporarily add the fully-qualified class name to your log4j appender format (%C if you're using a formatter based on PatternLayout). That gives you the package and class names of the source of the logging statements you want to exclude.
Then follow the example from the log4j manual to specifically raise the logging level of the packages/classes you don't want to see (search the page for text from the snippet below to find it on the page for more context):
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN
Don't forget to take the %C out of your pattern when you've excluded all the packages you don't want to see.
Here is a log4j configuration, I'm using with log4j:
# Log4J logger file
log = ./log4j
log4j.rootLogger = WARN, CONSOLE
log4j.logger.com.rizze.db=INFO
# Direct log messages to stdout
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss SSS} %-5p %m / %c{1}:%L %n
What is doing this work around
- show Logs with level >= INFO for package com.rizze.db
- show others Logs with level >= WARN
it is work for me.
add this line to you log4j configuration to fix this problem.
log4j.logger.org.mongodb.driver=ERROR