You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "AmanDaharwal (via GitHub)" <gi...@apache.org> on 2024/01/12 12:28:56 UTC

[I] Unable to get log4j logs in Console after migration from log4.1.x to log4j.2.x for dependent projects (logging-log4j2)

AmanDaharwal opened a new issue, #2185:
URL: https://github.com/apache/logging-log4j2/issues/2185

   ## Description
   
   I am importing a project X as a gradle dependency who was build on log 4j version 2.22.0 I am using the sample log4j 2.22.0
   After migration from log4j 1.x to log4j.2x, in my current project Y, I am not getting logs in CONSOLE. Though logs are printed properly in file( using rolling appender) but not in console.
   
   Project X is importing log4j.2x dependencies from another Maven project Z
   
   ## Configuration
   
   **Version:** Log 4j 2.22.0 ,  Intellij Idea IDE
   
   **Operating system:** Mac 12.3
   
   **JDK:** OpenJDK 11
   
   ## Reproduction
   
   1. Clone repo :`https://github.com/AmanDaharwal/getting-started-with-teswiz/tree/log4jTest`
   2. Checkout branch `log4jTest`
   3. Build gradle ( `./gradlew build` )
   4. Sample test is available at location java/com/znsio/sample/e2e/steps/Log4jTestStep.java. And two log4j and log4j2 properties files are available at src/test/resources/
       To run with log4j.properties file, use command - CONFIG=./configs/indigo/indigo_local_config.properties PLATFORM=web TAG=test1 ./gradlew run
      To run with log4j2.properties file, use command - CONFIG=./configs/indigo/indigo_local_config2.properties PLATFORM=web TAG=test1 ./gradlew run
   
   ## Behaviour
   1. With old log4j file, getting logs in file( at location target/{CurrentTimeStamp/testLogs}), but not in console 
   2. With new log4j2 file, not getting any logs
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Unable to get log4j logs in Console after migration from log4.1.x to log4j.2.x for dependent projects (logging-log4j2)

Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on issue #2185:
URL: https://github.com/apache/logging-log4j2/issues/2185#issuecomment-1891824166

   @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?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Unable to get log4j logs in Console after migration from log4.1.x to log4j.2.x for dependent projects (logging-log4j2)

Posted by "AmanDaharwal (via GitHub)" <gi...@apache.org>.
AmanDaharwal commented on issue #2185:
URL: https://github.com/apache/logging-log4j2/issues/2185#issuecomment-1891774887

   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 ?``


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Unable to get log4j logs in Console after migration from log4.1.x to log4j.2.x for dependent projects (logging-log4j2)

Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on issue #2185:
URL: https://github.com/apache/logging-log4j2/issues/2185#issuecomment-1889520926

   @AmanDaharwal, also note that you can use `log4j2.debug` property to troubleshoot Log4j issues such as _"Is my `FooBarLog4j2.xml` configuration loaded?"_


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Unable to get log4j logs in Console after migration from log4.1.x to log4j.2.x for dependent projects (logging-log4j2)

Posted by "AmanDaharwal (via GitHub)" <gi...@apache.org>.
AmanDaharwal commented on issue #2185:
URL: https://github.com/apache/logging-log4j2/issues/2185#issuecomment-1891357778

   @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);
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Unable to get log4j logs in Console after migration from log4.1.x to log4j.2.x for dependent projects (logging-log4j2)

Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on issue #2185:
URL: https://github.com/apache/logging-log4j2/issues/2185#issuecomment-1891855729

   @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())
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Unable to get log4j logs in Console after migration from log4.1.x to log4j.2.x for dependent projects (logging-log4j2)

Posted by "AmanDaharwal (via GitHub)" <gi...@apache.org>.
AmanDaharwal commented on issue #2185:
URL: https://github.com/apache/logging-log4j2/issues/2185#issuecomment-1892042182

   Thanks @vy for help, I missed your above message, this helped me resolved my Issue.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Unable to get log4j logs in Console after migration from log4.1.x to log4j.2.x for dependent projects (logging-log4j2)

Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on issue #2185:
URL: https://github.com/apache/logging-log4j2/issues/2185#issuecomment-1889464464

   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](/AmanDaharwal/getting-started-with-teswiz/blob/log4jTest/src/test/resources/log4j2.properties) and [Log4jTestStep.java](https://github.com/AmanDaharwal/getting-started-with-teswiz/blob/log4jTest/src/test/java/com/znsio/sample/e2e/steps/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.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Unable to get log4j logs in Console after migration from log4.1.x to log4j.2.x for dependent projects (logging-log4j2)

Posted by "AmanDaharwal (via GitHub)" <gi...@apache.org>.
AmanDaharwal closed issue #2185: Unable to get log4j logs in Console after migration from log4.1.x to log4j.2.x for dependent projects
URL: https://github.com/apache/logging-log4j2/issues/2185


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Unable to get log4j logs in Console after migration from log4.1.x to log4j.2.x for dependent projects (logging-log4j2)

Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on issue #2185:
URL: https://github.com/apache/logging-log4j2/issues/2185#issuecomment-1891626589

   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")`?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org