Getting issue in log4j API logs generation - java

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

Related

Log4j does literally nothing?

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/

Why log4j won't create file?

i am trying to create a .log file for my app.
logs are going out to the console, but not on file.
Here is my .properties file:
log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/Users/user/Downloads/demo/src/main/resourceslogigng.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
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
This is the maven dependency:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
and this is the injection in the #RestController class:
//SFL4J
Logger logger = LoggerFactory.getLogger(UserController.class);
User user = new ObjectMapper().readValue(message, User.class);
logger.info("New message read successfully!");

Log4j does not create the logfile

I want to use the unusuable log4J for logfiles in a java maven application. But the logfile is not created.
This is my a part of my .pom file:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
This is my log4j.properties file (under src/main/resources):
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=E:\\Test\\logfile.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
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
# Direct log messages to 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
In my main method:
log.debug("debug");
log.info("info");
log.warn("warn");
log.error("error");
The log messages are in the console log, but the logfile has not been created. What am I doing wrong?
your pom.xml is not correct. it has to be <dependency> in the first line. (missing >)
Apart from that, your log4j.properties should do the work, I tried it and it writes a log-file.
How do you create the Logger instance log?
Do you have writing access to E:\\Test\\logfile.log?

logj4 with Spring MVC 3.2.8 application

I have an application based in the Spring Web model-view-controller (MVC) framework using logj4. Here my log4j.properties
log4j.rootCategory=INFO, console, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601}] %5p [%t] %x (%C:%L) - %m%n
org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=logs/noentenimnicinc.log
log4j.appender.logfile.MaxFileSize=1512KB
log4j.appender.logfile.Threshold=INFO
log4j.appender.logfile.Append=true
log4j.appender.logfile.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.logfile.RollingPolicy.FileNamePattern=ecat_admin.%d{yyyy-MM-dd-HH}.gz
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=10
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{ISO8601}] %5p [%t] %x (%C:%L) - %m%n
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=CONSOLE
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ISO8601}] %5p [%t] %x (%C:%L) - %m%n
and my controller:
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
#Controller
public class NoEnTenimNiCinc {
private static final Logger LOGGER = Logger.getLogger (NoEnTenimNiCinc.class);
private void fer () {
LOGGER.info("***************************************************************");
LOGGER.info(" SUBMITTING THE APPLICATION ");
LOGGER.info("***************************************************************");
System.out.println ("System.out***************************************************************");
System.out.println ("System.out SUBMITTING THE APPLICATION ");
System.out.println ("System.out***************************************************************");
}
}
As far as I understand I should see in the eclipse console all the messages but I only see the ones generated by System.out.println
The ConsoleAppender has threshold field inherited from class org.apache.log4j.AppenderSkeleton. Threshold is a Prority Object and its possible values are:
DEBUG
ERROR
FATAL
INFO
WARN
...
Console is not a possible value for it.

java.lang.Class:ERROR while initializing log4j properties file in java program

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.

Categories