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!");
Related
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 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?
I'm trying to use Apache log4j in Tomcat 7. I need the logs in DEBUG mode so I have used
log4j.rootLogger=DEBUG, stdout, file
I have also used file rolling so as to get the logs daily in a separate file.
log4j.appender.file=org.apache.log4j.RollingFileAppender
but in the DEBUG mode all the debug related logs are logging into "catalina.out" file instead of the daily created log, I cannot see any DEBUG related logs in the daily created log file only INFO and SEVERE are present in it. And my another question is how to not to log everything into catalina.log file as the file size is keep on increasing
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.MaxFileSize=5MB
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
Dependencies that I have used:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
If you are working in windows environment please add below line code to your File logging configuration:
log4j.appender.file.File=${catalina.home}\\yourLog.log
This will direct to tomcat->bin folder. If you want specifically in tomcat->logs folder please configure accordingly by going one step backwards.
It is because The Apache Tomcat has a custom version of Log4J (referenced as JULI) built in it.
From the Tomcat Documentation:
The internal logging for Apache Tomcat uses JULI, a packaged renamed
fork of Apache Commons Logging that, by default, is hard-coded to use
the java.util.logging framework. This ensures that Tomcat's internal
logging and any web application logging will remain independent, even
if a web application uses Apache Commons Logging.
You could use:
log4j.rootLogger=TRACE, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.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 %c{1}:%L - %m%n
I am creating a Spring Boot Application in STS (Spring Tool Suite).
The problem occurs when trying to log using log4j. It does not create the log file in the system, though it does log on the console. I tried various methods given in the forum(including checking case-sensitivity, , but I think I am going wrong somewhere. An elaborate response would be really helpful.
I have a dependency in the pom.xml file for the log4j jar:-
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
In my log4j.properties file, I am trying to have logs in console as well as a file
# Root logger option
log4j.rootLogger=ALL, stdout, file
# Redirect log messages to console
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
# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log4j.log
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.FILE.layout.conversionPattern=%m%n
log4j.appender.file.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Java controller where I want to log (IndexController.java)
package com.project.Controller;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class IndexController {
final static Logger logger = Logger.getLogger(IndexController.class.getName());
#RequestMapping("/")
String index(Model model) {
logger.info("in index");
return "index";
}
}
Please comment if you need any further details. Thanks
Create a dependency for spring-boot-starter-log4j and exclude spring-boot-starter-logging in your pom.xml.Make the following entry in your pom.xml:
<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-log4j</artifactId>
</dependency>
This should create the log file and log the details.
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