Not Every log is printing in Log files using LOG4j2 Properties file - java

I have configured my webapp in java using log4j2 for logs using properties file.
When I am starting tomcat ,there are so many logs printing in console but in my file only few lines are printing.
What should I do in order to print every line which is present in console to my Log File.
Recently I have migrated from log 1.x to log 2.16
When I am using log1.x everything is printed in log files which are in console but in log2 its not printing.
For Example when I am staring tomcat ,I am getting only my app related logs in the file but not anything related to tomcat startup logs or any other jar related logs
Can anyone help me on this
below is my config file
log4j2 properties:
status = all
appender.console.type = Console
appender.console.name = LogToConsole
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 = LogToFile
#appender.file.fileName=logs/app.log
#appender.file.layout.type=PatternLayout
#appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
# Rotate log file
appender.rolling.type = RollingFile
appender.rolling.name = LogToRollingFile
appender.rolling.fileName = ${catalina.home}/logs/SunnyLogs.log
appender.rolling.filePattern = ${catalina.home}/logs/$${date:yyyy-MM}/SunnyLogs-%d{MM-dd-yyyy}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 10
# Log to console and rolling file
logger.app.name = com.sunny.common
logger.app.level = all
logger.app.additivity = false
logger.app.appenderRef.rolling.ref = LogToRollingFile
logger.app.appenderRef.console.ref = LogToConsole
rootLogger.level = all
rootLogger.appenderRef.stdout.ref = LogToConsole

After deep Research I have found one Jar as my solution
Log4j-jcl-2.16 jar.
After adding this to my build path I am getting all Logs printed in my file

-Dlog4j.configurationFile=/src/main/resources/log4j2.xml in setEnv.bat file solved my problem.

Related

log4j2 in Tomcat8 App works in IDE (Eclipse) but not in Production environment (RHEL8)

I have a Tomcat 8.5 Webapp that I've developed. In Development I run Tomcat (8.5) via my IDE (Eclipse), and the logging configuration is picked up from WEB-INF/classes/log4j2.properties
When I build the War and deploy to our RHEL8 servers running Tomcat8.5, there is no log file created at all.
I presume there is some kind of classpath difference that's leading to this behaviour; but how do I diagnose the issue?
Included in my WebApp are the logging jars:
log4j-core-2.15.0.jar
log4j-jcl-2.15.0.jar
log4j-api-2.15.0.jar
log4j-1.2-api-2.15.0.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
I've tried logging in my app using both:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
private static final Logger log = LoggerFactory.getLogger(ApplicationConfiguration.class);
log.info("Application Initialised");
and:
import org.apache.log4j.Logger;
public static Logger log = Logger.getLogger(DashboardBean.class);
log.info("Test");
but neither one results in the file being created
log4j2.properties:
name=PropertiesConfig
property.filename = tms.log
appenders = rolling
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = tms-backup-%d{yyyy-MM-dd}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 50
loggers = rolling
logger.rolling.name = com.mycompany
logger.rolling.level = info
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

LogManager ClassNotFoundException when upgrade Log4j to Logj42

I should upgrade my projects log4j to log4j2.So i deleted log4j dependencies and add log4j2.Some project worked well but one project get error as follow:
]] Root cause of ServletException.
java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
at jsp_servlet.__view._jspService(__view.java:86)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:35)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:286)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:260)
Truncated. see log file for complete stacktrace
Caused By: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
at weblogic.utils.classloaders.GenericClassLoader.findLocalClass(GenericClassLoader.java:1029)
at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClassLoader.java:990)
at weblogic.utils.classloaders.ChangeAwareClassLoader.findClass(ChangeAwareClassLoader.java:104)
at weblogic.utils.classloaders.GenericClassLoader.doFindClass(GenericClassLoader.java:611)
at weblogic.utils.classloaders.GenericClassLoader.loadClass(GenericClassLoader.java:543)
Truncated. see log file for complete stacktrace
I used mvn dependency:tree command and tried to find some conflict between dependencies and
i tried to exclude every child library that might be relevant,but still get same error.
code :
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
private static final Logger log = LogManager.getLogger(View.class);
dependencies:
Maven: commons-io:commons-io:2.8.0
Maven: javax.servlet:servlet-api:2.5
Maven: oracle:ojdbc6:11.2.0.3
Maven: org.apache.logging.log4j:log4j-api:2.14.0
Maven: org.apache.logging.log4j:log4j-core:2.14.0
Maven: org.apache.pdfbox:fontbox:2.0.23
Maven: org.apache.pdfbox:pdfbox:2.0.23
Maven: org.springframework:spring-beans:5.2.12.RELEASE
Maven: org.springframework:spring-core:5.2.12.RELEASE
Maven: org.springframework:spring-web:5.2.12.RELEASE
log4j2.properties:
status = error
name = PropertiesConfig
#Make sure to change log file path as per your need
property.filename = path/to/filename
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = console,rolling
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %5p - %d{yyyy-MM-dd HH:mm:ss.SSS} - %F:%L - %m%n
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = '-'yyyy-MM-dd'.log'
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %5p - %d{yyyy-MM-dd HH:mm:ss.SSS} - %F:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
loggers = rolling
#Make sure to change the package structure as per your application
logger.rolling.name = com.thy.dvs
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
i found the problem at least as below picture.
When I add new dependency to pom.xml, I also had to add dependencies "available elements" to my ear/war packages.

Upgrading log4j 1.2.14 to 2.12.1 Error No Root logger was configured and No log file created

We are upgrading log4j 1.2.14 to 2.12.1 and I have the log4j2.properties file as below
status = error
log4j2.appender.console.type = Console
log4j2.appender.console.name = LogToConsole
log4j2.appender.console.layout.type = PatternLayout
log4j2.appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
#log4j2.appender.file.type = File
#log4j2.appender.file.name = LogToFile
#log4j2.appender.file.fileName=logs/app.log
#log4j2.appender.file.layout.type=PatternLayout
#log4j2.appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
# Rotate log file
log4j2.appender.rolling.type = RollingRandomAccessFile
log4j2.appender.rolling.name = LogToRollingRandomAccessFile
log4j2.appender.rolling.fileName = ${server.home}/logs/server.log
log4j2.appender.rolling.filePattern = logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz
log4j2.appender.rolling.layout.type = PatternLayout
log4j2.appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
log4j2.appender.rolling.policies.type = Policies
log4j2.appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
log4j2.appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.rolling.policies.size.size=10MB
log4j2.appender.rolling.strategy.type = DefaultRolloverStrategy
log4j2.appender.rolling.strategy.max = 10
# Log to console and rolling file
#logger.app.type = AsyncLogger
log4j2.logger.app.name = com.ssl.server
log4j2.logger.app.level = info
log4j2.logger.app.additivity = false
log4j2.logger.app.appenderRef.rolling.ref = LogToRollingRandomAccessFile
log4j2.logger.app.appenderRef.console.ref = LogToConsole
log4j2.rootLogger.level = info
log4j2.rootLogger.appenderRef.stdout.ref = LogToConsole
I have below dependancies in my pom.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
I am initializing the logger as below
private static final org.apache.logging.log4j.Logger LOG = org.apache.logging.log4j.LogManager.getLogger(SSLServer.class.getName());
When I start the standalone Java application using the main() am getting below error and server.log file is not created, I did some research regarding the below error and most of the articles were referring to log4j2.xml configuration and none of the config changes worked in my log4j2.properties
log warn: org.apache.logging.log4j.status.StatusLogger - No Root logger was configured, creating default ERROR-level Root logger with Console appender
No Root logger was configured, creating default ERROR-level Root logger with Console appender
Few things to look at:
make sure you don't have any other log4j2 config files on your classpath. The list is not small and can trip up. See it in the documentation here.
get debug info out of log4j2 by increasing status messages: status = info (debug or trace )
remove log4j2. as the prefix from all your properties so that log4j2.appender.console.type = Console becomes appender.console.type = Console
add file logging to your rootlogger: rootLogger.appenderRef.rolling.ref = LogToFile
I adapted your configuration to work. Please check this out:
# Print configuration errors only
status=error
# Console appender
appender.console.type=Console
appender.console.name=LogToConsole
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
# Rotate log file
appender.rolling.type=RollingRandomAccessFile
appender.rolling.name=LogToRollingRandomAccessFile
appender.rolling.fileName=${SERVER_HOME}/logs/server.log
appender.rolling.filePattern=logs/${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz
appender.rolling.layout.type=PatternLayout
appender.rolling.layout.pattern=%d %p %C{1.} [%t] %m%n
appender.rolling.policies.type=Policies
appender.rolling.policies.time.type=TimeBasedTriggeringPolicy
appender.rolling.policies.size.type=SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=1KB <-- for testing only!
appender.rolling.strategy.type=DefaultRolloverStrategy
appender.rolling.strategy.max=10
# Root logger
rootLogger.level=info
rootLogger.appenderRef.console.ref=LogToConsole
rootLogger.appenderRef.rolling.ref=LogToRollingRandomAccessFile
Please make sure you have the SERVER_HOME variable in your environment.
I don't see where you are declaring a root logger in you log4j.properties file.
I see, at the bottom, where you are setting the "level" on the rootlogger but I don't see where you've established the rootLogger.
My log4j.properties file start out like this:
log4j.rootLogger=WARN, FILE, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %20t %25F:%L - %m%n
I think you need two modifications in your properties configuration:
Remove the prefix "log4j2."
Remove the space after "appender.rolling.type =
RollingRandomAccessFile "
Also check if your package name is correct:
logger.app.name = com.ssl.server
status = error
appender.console.type = Console
appender.console.name = LogToConsole
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 = LogToFile
#appender.file.fileName=logs/app.log
#appender.file.layout.type=PatternLayout
#appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
# Rotate log file
appender.rolling.type = RollingRandomAccessFile
appender.rolling.name = LogToRollingRandomAccessFile
appender.rolling.fileName = logs/server.log
appender.rolling.fileName = ${server.home}/logs/server.log
appender.rolling.filePattern = logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 10
# Log to console and rolling file
#logger.app.type = AsyncLogger
logger.app.name = com.ssl.server
logger.app.level = info
logger.app.additivity = false
logger.app.appenderRef.rolling.ref = LogToRollingRandomAccessFile
logger.app.appenderRef.console.ref = LogToConsole
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = LogToConsole

How do I print IST in log file using log4j2?

I want to print IST in my log file using log4j2. Currently it is printing time in UTC. Below is my log4j2.properties file.
name = log4j2
appenders = console, file
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=/home/dockerdev/code/stmtParser/log/fcu_analyzer.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = EnhancedPatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss}{IST} %-5p %c{1}:%L - %m%n
loggers=file
logger.file.name=/home/dockerdev/code/stmtParser/log4j2.properties
logger.file.level = info
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = debug
rootLogger.appenderRefs = file ,stdout
rootLogger.appenderRef.file.ref = LOGFILE
rootLogger.appenderRef.console.ref = STDOUT
This I am trying to run in local docker. Log generated from this shows UTC. How can i get IST ? what change is required?
Try change timezone inside your Dockerfile
I got the solution, since i was using log4j2 I had to only add {IST} after date pattern for file appender. So final properties file which would print IST in log file is
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=/home/dockerdev/code/stmtParser/log/fcu_analyzer.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS}{IST} [%t] %c{1} - %msg%n

log4j does not log for file

I am using log4j to log to both console and file. It just works with console, but not with file, thought the file is being created
This is my configuration 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=${filename}/my_server_logs.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=guru.springframework.blog.log4j2properties
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = debug
rootLogger.appenderRefs = file, stdout
rootLogger.appenderRef.stdout.ref = STDOUT
What am I missing please?
The connection between file appender and root logger is missing.
Add the following line at the bottom of your properties file:
rootLogger.appenderRef.file.ref = LOGFILE
It will work as expected.
I think you expect that the variable ${filename} be substituted with the value of property.filename.
Can you try to substitute ${filename} with a hard path and try again (E.g: 'C:\' on Windows or '/tmp/' on Unix-like).

Categories