My configuration file is path of the class path. At least I thought it is. I placed the log4j.properties file in the resources folder and log4j does nothing with it. Even if I delete it, no error occurs.
As anyone can see I'm using maven
Content of LoggerTest:
package com.dersimi.stella.logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class LoggerTest {
public static void main(String[] args) {
System.out.println("xxx");
Logger logger = LogManager.getLogger(LoggerTest.class);
logger.info("Hello this is an info message");
System.out.println("xxx");
}
}
Program output:
xxx
xxx
Content of log4j.properties:
log4j.rootLogger=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=INFO
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%n
In pom.xml is nothing out of the ordinary, just the dependency org.apache.logging.log4j log4j-core 2.17.2, compiler source target is 16, no plugins
The main problem is that you are trying to use log4j (the first one) configurations for log4j2.
In first place make sure you have the following dependencies:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.0</version>
</dependency>
Second, have a file named log4j2.properties with a content like this:
status = warn
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
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = LogToConsole
Reference: https://mkyong.com/logging/log4j2-properties-example/
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 have a Spring boot app and I want to use LOG4J2 to manage my logs in different files, but the logfile isn't created.
I have a log4j2.properties configuration under "resources"
My log file configuration:
name=PropertiesConfig
#output folder
property.filename=G:\\PRUEBAS
property.packagename=com.package.main
appenders=console, infoLoggerAppender, commonLoggerAppender, errorLoggerAppender
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#common appender
appender.commonLoggerAppender.type=File
appender.commonLoggerAppender.name=RollingFile
appender.commonLoggerAppender.fileName=${filename}\\log.log
appender.commonLoggerAppender.layout.type=PatternLayout
appender.commonLoggerAppender.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#error appender
appender.errorLoggerAppender.type=RandomAccessFile
appender.errorLoggerAppender.name=RandomAccessFile
appender.errorLoggerAppender.fileName=${filename}\\error.log
appender.errorLoggerAppender.layout.type=PatternLayout
appender.errorLoggerAppender.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#using LevelRangeFilter to only log error levels.
appender.errorLoggerAppender.filter.threshold.type=LevelRangeFilter
appender.errorLoggerAppender.filter.threshold.minLevel=error
appender.errorLoggerAppender.filter.threshold.maxLevel=error
#info appender
appender.infoLoggerAppender.type=File
appender.infoLoggerAppender.name=LOGFILE
appender.infoLoggerAppender.fileName=${filename}\\info.log
appender.infoLoggerAppender.layout.type=PatternLayout
appender.infoLoggerAppender.layout.info=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#using LevelRangeFilter to only log info levels.
appender.infoLoggerAppender.filter.threshold.type=LevelRangeFilter
appender.infoLoggerAppender.filter.threshold.minLevel=info
appender.infoLoggerAppender.filter.threshold.maxLevel=info
# creating only one logger, we can use this with multiple appenders.
loggers=fileLogger
# this is package name. This package and all of it's child packages will use this logger
logger.fileLogger.name=${packagename}
# logger base level
logger.fileLogger.level=debug
logger.fileLogger.appenderRefs=infoLoggerAppender, commonLoggerAppender, errorLoggerAppender
logger.fileLogger.appenderRef.infoLoggerAppender.ref=LOGFILE
logger.fileLogger.appenderRef.commonLoggerAppender.ref=RollingFile
logger.fileLogger.appenderRef.errorLoggerAppender.ref=RandomAccessFile
rootLogger.level=debug
rootLogger.appenderRefs=stdout
rootLogger.appenderRef.stdout.ref=STDOUT
What can I do? Someone can help me?
I waste my last two days with this...
Check the log4j2 jar is available in sprint boot application if not then add the maven dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Create Log4j2.properties file and declare all the
Create that file inside the resource folder
name = ABC
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ABClog/ABCapplication.log
appender.rolling.filePattern = ABClog/ABCapplication.%d{dd-MMM}.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l]- %msg%n
appender.rolling.policies.type = Policies
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.action.type = Delete
appender.rolling.strategy.action.basepath =log
appender.rolling.strategy.action.condition.type = IfFileName
appender.rolling.strategy.action.condition.glob = ABC*.log
appender.rolling.strategy.action.ifAny.type = IfAny
appender.rolling.strategy.action.ifAny.ifLastModified.type = IfLastModified
appender.rolling.strategy.action.ifAny.ifLastModified.age = 14d
appender.rolling.strategy.action.ifAny.ifAccumulatedFileSize.type = IfAccumulatedFileSize
appender.rolling.strategy.action.ifAny.ifAccumulatedFileSize.exceeds = 200MB
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
logger.rolling.name = com.currentobject.controller
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
provide the path of log4j2.properties file into the application.properties file
logging.config=classpath:properties/log4j2.properties
I am facing issue in generating logs on console.
Below is the snippents I am trying with :-
pom.xml:-
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
log4j.properties file :-I have kept this in src/main/resources folder
#Set level
log4j.rootCategory=debug, console, file
# Appender which writes to console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{MM-dd-yyyy HH:mm:ss} %F %-5p [%t] %c{2} %L - %m%n
# Appender which writes to a file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\eclipse-workspace\CucumberWithTestNGForSelenium\application.log
# Defining maximum size of a log file
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %5p [%t] %c{1}:%L - %m%n
log4j.appender.file.Append=true
And,below is my Test File
package stepDefinition;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class GenerateLogs {
final static Logger log=Logger.getLogger(GenerateLogs.class);
#Test
public void Test1()
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Mkap\\Downloads\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
log.info("Launching chrome browser");
driver.get("http://google.com");
}
#Test
public void Test2()
{
System.out.println("Yeh!google has opened");
}
}
Can you please help why logs are not getting generated on console. Also,why am not able to resolve errors of Logger class.
Try with root logger instead of rootCategory
log4j.rootLogger=DEBUG,console,file
Find documentation here http://logging.apache.org/log4j/1.2/manual.html
I am trying to run a standalone java program using log4j, but receiving below while debugging (with no log4j related logs on console):
log= {Logger#1343} "java.lang.Class:ERROR in 18b4aac2"
Can someone please suggest what is wrong here?
The code is as below:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import java.io.IOException;
import java.sql.SQLException;
public class log4jExample {
static org.apache.logging.log4j.Logger log = LogManager.getLogger(log4jExample.class.getClass());
public static void main(String[] args)throws IOException,SQLException {
System.out.println("in main...");
log.debug("Hello this is a debug message");
System.out.println("in main...2..");
log.info("Hello this is an info message");
}
}
And the log4j.properties file is as below which is kept at src/main/resources.
log4j.rootLogger=DEBUG, stdout, file
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
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\test\\log4j-example.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
I am running my java program with VM option
-Dlog4j.configurationFile=C:\MyFirstProject\src\main\resources\log4j.properties
It seems to me like you have mixed up log4j versions. The configuration file you use is using log4j 1.x format. You have to convert it to log4j 2.x format. I tried your example and it works. See details below.
pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.bft.</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
</project>
Re-factored code
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class log4jExample {
static final Logger logger = LogManager.getLogger(log4jExample.class);
public static void main(String[] args) {
System.out.println("in main...");
logger.info("Hello this is a debug message");
System.out.println("in main...2..");
logger.info("Hello this is an info message");
}
}
log4j.properties file (in log4j2.x format)
status = error
dest = err
name = PropertiesConfig
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %m%n
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT
Output:
in main...
Hello this is a debug message
in main...2..
Hello this is an info message
Process finished with exit code 0
Refer the log4j docs.
It is not an error !
It just says that your logger level is set to ERROR, so you will not see the messages logged to DEBUG/INFO levels.
Maybe you should check if your configuration is compatible with Log4J-2.x:
https://logging.apache.org/log4j/2.x/manual/configuration.html#Properties
https://logging.apache.org/log4j/2.x/manual/appenders.html#ConsoleAppender
Use XML format is usually better.
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