Comments (8)
This is now working for me after using LoggerContext instead of PropertyConfigurator.
LoggerContext context = (LoggerContext) LogManager.getContext(false);
File file = new File("./resources/log4j2.properties");
context.setConfigLocation(file.toURI());
But, now the challenge is log4j2.properties is unable to identify system variable ${LOG_DIR} in below properties file -
status = warn
name= properties_configuration
# Give directory path where log files should get stored
property.basePath = ${LOG_DIR}
# ConsoleAppender will print logs on console
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
# Specify the pattern of the logs
appender.console.layout.pattern = %5p [%t] (%F:%L)- %m%n
# RollingFileAppender will print logs in file which can be rotated based on time or size
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= ${basePath}teswizSampleTestLog.log
appender.rolling.filePattern= ${basePath}teswizSampleTestLog_%d{yyyyMMdd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d - %c -%p - %m%n
appender.rolling.policies.type = Policies
# Configure root logger for logging error logs in classes which are in package other than above specified package
rootLogger.level = info
rootLogger.additivity = false
rootLogger.appenderRef.rolling.ref = fileLogger
rootLogger.appenderRef.console.ref = consoleLogger
@vy any idea, how to make it work with LoggerContext or Configurator ?``
from logging-log4j2.
Hello @AmanDaharwal! I don't know what kind of a help you are looking for, but I don't find it safe to clone a repository from an unknown source and run it on my machine. Could you provide us a more minimalist example, please? That is, your Log4j configuration and a sample Java file to trigger the issue.
Until that time, I will try to help as follows. I see your log4j2.properties and Log4jTestStep.java. I adopted these into following log4j2.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss,SSS}] %-5p [%c.%M()] (%t) %m%n"/>
</Console>
<File name="File" fileName="${env:LOG_DIR}/foo.log">
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss,SSS}] %-5p [%c.%M()] (%t) %m%n"/>
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
and Log4j2Issue2185.java
:
package ci.yazi.volkan;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log4j2Issue2185 {
private static final Logger LOGGER = LogManager.getLogger();
public static void main(String[] args) {
LOGGER.info("INFO ->>>>>>>>>>>>>>>");
LOGGER.trace("TRACE ->>>>>>>>>>>>>>>");
LOGGER.debug("DEBUG ->>>>>>>>>>>>>>>");
LOGGER.warn("WARN ->>>>>>>>>>>>>>>");
LOGGER.fatal("FATAL ->>>>>>>>>>>>>");
LOGGER.error("ERROR ->>>>>>>>>>>>>>");
}
}
It all works fine. If you can provide a minimalist example reproducing the issue in a way I shared above, we can help better.
from logging-log4j2.
@AmanDaharwal, also note that you can use log4j2.debug
property to troubleshoot Log4j issues such as "Is my FooBarLog4j2.xml
configuration loaded?"
from logging-log4j2.
@vy Previously we were using log4j 1.x references for logging (org.apache.log4j ) and log4j 2.x dependency (Log 4j 2.22.0). We were using below code to make it compatible
private static void setLog4jCompatibility() {
// Migrating from Log4j 1.x to 2.x - https://logging.apache.org/log4j/2.x/manual/migration.html
System.setProperty("log4j1.compatibility", "true");
}
Now, we updated our log4j 1.x references with log4j 2.x ( org.apache.logging.log4j) with same log4j 2.x dependency (Log 4j 2.22.0). Also remove the above compatibility code.
With old log4j.properties files( mentioned below), we are not getting logs on CONSOLE, though they getting logged properly in file
# root logger
log4j.rootLogger=INFO,CONSOLE,R
# appender
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.R=org.apache.log4j.RollingFileAppender
#log file location
log4j.appender.R.File=${LOG_DIR}/testLogs/teswizSampleTestLog.log
# layout and pattern
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L)- %m%n
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c -%p - %m%n
log4j.appender.R.Append=false
Also, tried updating to log4j2.properties file, but no logs are getting logged at CONSOLE or FILE.
status = error
dest = err
name = PropertiesConfig
property.filename = ${LOG_DIR}/testLogs/teswizSampleTestLog.log
appenders = console, rolling
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{yyyy-MM-dd HH:mm:ss,SSS}] %-5p [%c.%M()] (%t) %m%n
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${LOG_DIR}/testLogs/teswizSampleTestLog.log
appender.rolling.filePattern = $%d{MM-dd-yy-HH-mm-ss}
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%d{yyyy-MM-dd HH:mm:ss,SSS}] %-5p [%c.%M()] (%t) %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
rootLogger.level = info
rootLogger.appenderRefs = console, rolling
rootLogger.appenderRef.console.ref = STDOUT
rootLogger.appenderRef.rolling.ref = RollingFile
# The root logger with appender name
rootLogger = INFO, STDOUT
Also, using the PropertyConfigurator to configure both files
Path logFilePath = Paths.get(properties.get(LOG_PROPERTIES_FILE).toString());
configs.put(LOG_PROPERTIES_FILE, logFilePath.toString());
inputStream = Files.newInputStream(logFilePath);
PropertyConfigurator.configure(inputStream);
from logging-log4j2.
In your log4j2.properties
, the following block is problematic:
rootLogger.level = info
rootLogger.appenderRefs = console, rolling
rootLogger.appenderRef.console.ref = STDOUT
rootLogger.appenderRef.rolling.ref = RollingFile
# The root logger with appender name
rootLogger = INFO, STDOUT
The last line overrides the first block – RollingFile
appender disappears. I have refactored your configuration below.
Given this Log4j2Issue2185.properties
status = WARN
property.fileNamePrefix = ${sys:LOG_DIR}/Log4j2Issue2185
appender.console.type = Console
appender.console.name = CONSOLE
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{yyyy-MM-dd HH:mm:ss,SSS}] %-5p [%c.%M()] (%t) %m%n
appender.rolling.type = RollingFile
appender.rolling.name = ROLLING
appender.rolling.fileName = ${fileNamePrefix}.log
appender.rolling.filePattern = ${fileNamePrefix}-%d{MM-dd-yy-HH-mm-ss}.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%d{yyyy-MM-dd HH:mm:ss,SSS}] %-5p [%c.%M()] (%t) %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size = 100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
rootLogger.level = INFO
rootLogger.appenderRef.$1.ref = CONSOLE
rootLogger.appenderRef.$2.ref = ROLLING
Following Log4j2Issue2185.java
works for me:
package ci.yazi.volkan;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log4j2Issue2185 {
static {
System.setProperty("LOG_DIR", "/tmp");
System.setProperty("log4j.configurationFile", "Log4j2Issue2185.properties");
}
private static final Logger LOGGER = LogManager.getLogger();
public static void main(String[] args) {
LOGGER.info("INFO ->>>>>>>>>>>>>>>");
LOGGER.trace("TRACE ->>>>>>>>>>>>>>>");
LOGGER.debug("DEBUG ->>>>>>>>>>>>>>>");
LOGGER.warn("WARN ->>>>>>>>>>>>>>>");
LOGGER.fatal("FATAL ->>>>>>>>>>>>>");
LOGGER.error("ERROR ->>>>>>>>>>>>>>");
}
}
I need you to share a similar Java file reproducing the issue you are describing.
Also, using the PropertyConfigurator to configure both files
Why? This is a Log4j 1 configurator. Can't you use it the way I exemplified above, i.e., using System.setProperty("log4j.configurationFile", "Log4j2Issue2185.properties")
?
from logging-log4j2.
@AmanDaharwal, have you read my message? Have you seen the Java code I shared there? Did you see how I pass the configuration file and LOG_DIR
using System.setProperty()
? Have you tried that?
from logging-log4j2.
@AmanDaharwal, I have the impression that you haven't read my messages completely – I might be wrong. Please read my earlier messages and use System.setProperty()
I shared there.
If you insist on using a Configurator
, of which I wouldn't recommend since it implies a compile-time log4j-core
dependency, you can use
org.apache.logging.log4j.core.config.Configurator.reconfigure(new File("Log4j2Issue2185.properties").toURI())
from logging-log4j2.
Thanks @vy for help, I missed your above message, this helped me resolved my Issue.
from logging-log4j2.
Related Issues (20)
- Removing all keys from `ThreadContextMap` does not clear the `ThreadLocal`
- Revamp the `Configuration` page HOT 2
- Revamp the `Lookups` page
- Revamp the `Layouts` page
- Revamp the `Appenders` page HOT 1
- Revamp the `Filters` page
- Revamp the `Extending` page
- Revamp the `Plugins` page
- Create a `Troubleshooting` page HOT 1
- Revamp the `Performance` page
- Revamp the `Asynchronous logging` page
- Revamp the `API` page
- Uniform handling of location and URIs HOT 1
- [MS14] Documentation revamp 1/5 HOT 1
- [MS15] Documentation revamp 2/5
- [MS16] Documentation revamp 3/5
- [MS17] Documentation revamp 4/5
- [MS18] Documentation revamp 5/5
- Revamp the `Web Applications` page
- Implement uniform password "encryption/decryption" mechanism HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from logging-log4j2.