Log4j with Springboot not appending/creating file - java

My logger is printing out logs to my console, but I want it to append my .log file. I placed the following poperties inside my application.properties file:
log4j.rootLogger = DEBUG, stdout, stderr, dailyfile
log4j.appender.stdout.Threshold = INFO
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d] %5p %m (%C::%M:%L)%n
log4j.appender.stderr.Threshold = ERROR
log4j.appender.stderr = org.apache.log4j.ConsoleAppender
log4j.appender.stderr.layout = org.apache.log4j.PatternLayout
log4j.appender.stderr.layout.ConversionPattern=[%d] %5p %m (%C::%M:%L)%n
log4j.appender.dailyfile.Threshold = DEBUG
log4j.appender.dailyfile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyfile.File = ../webapps/test/a.log
log4j.appender.dailyfile.layout = org.apache.log4j.PatternLayout
log4j.appender.dailyfile.layout.ConversionPattern=[%d] %5p {%t} %m (%C::%M:%L)%n
log4j.appender.dailyfile.DatePattern ='.'yyyy-MM-dd
And I have a few log.info() calls in my code:
log.info("bla bla bla bla");
For now my logs are visible on the console. This code can't create a file somehow. Even if I create the file manually, it is not being updated (appended). What should I do in order for my code to work?

I didn't know, that file with Log4j properties has to be named exactly log4j.properties. In my case all I had to do was create file, place all properties inside and move this file to src/resources. Thanks for help guys!

Related

How to add rollingfileappender and dailyrollingfileappender in a single log4j properties

I was implementing log file based on the time and date using Log4j tool for my java application I am facing problem when i am including both file appender (RollingFileAppender and DaillyRollingFileAppender) in the same properties file. This is the properties configuration i was trying .any clue would be appreciated .
log=logs log4j.rootLogger = DEBUG, FILE
log4j.appender.FILE = org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File = log/log.txt log4j.appender.FILE.Append = true
log4j.appender.FILE.MaxFileSize = 1kb
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern = [%p] %d %c %M -%m%n%n
It should be
log4j.rootLogger = DEBUG, FILE
instead of
log=logs log4j.rootLogger = DEBUG, FILE
one property per line
log4j.rootLogger = DEBUG, FILE
log4j.appender.FILE = org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File = log/log.txt
log4j.appender.FILE.Append = true
log4j.appender.FILE.MaxFileSize = 1kb
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern = [%p] %d %c %M -%m%n%n

Log4j : is it required to have multiple logger objects for creating multiple log files?

i was trying to redirect the log to console and two different log files..
this is my log4j.properties file:
log4j.rootLogger = DEBUG, console, file, csv
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %d{HH:mm:ss} %5p [%t] - %c.%M - %m%n
log4j.appender.file = org.apache.log4j.FileAppender
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = %d %5p [%t] - %L %c.%M - %m%n
log4j.appender.file.File=./log/logfile.log
log4j.appender.csv = org.apache.log4j.FileAppender
log4j.appender.csv.layout = org.apache.log4j.PatternLayout
log4j.appender.csv.layout.ConversionPattern =%m%n
log4j.appender.csv.File = ./log/log.csv
now the problem is that the logging is happening, but both the files have both the logs that were meant to be logged separately in different files.. i really dont know where am i going wrong! can anyone please help me out??
Log4j does not easily allow for splitting logs between multiple files. The basic idea is that all log statements are considered equal and should be treated equally.
If you were using slf4j as the API you could easily switch the backend from log4j to logback which has SiftingAppender for exactly this purpose.
http://logback.qos.ch/manual/loggingSeparation.html

Log4J out to console and file produces warnings/errors log4j:WARN File option not set for appender

I'd like to integrate Log4J logging into my Java application, writing information out to a log file as well as displaying the info at the console. Unfortunately, I've had some trouble getting this off the ground. I'm working with Eclipse Juno on Win 7.
My log4j.properties file:
#configure logfile
log4j.appender.logfile = org.apache.log4j.RollingFileAppender
log4j.appedner.logfile.File = MyAppLog.log
log4j.appender.logfile.Append = true
log4j.appender.logfile.layout = org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n
#configure 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 = %-5p %d [%t][%F:%L] : %m%n
log4j.rootLogger = debug, logfile, stdout
I belive the above follows the pattern of some other accepted answers on this topic.
I've specifed the location of this log4j.properties file wtih a VM argument:
-Dlog4j.configuration=file:///${workspace_loc}\path\to\file\log4j.properties
I instantiate the logger in my java class:
static Logger logger = Logger.getLogger(MyApp.class.getName());
log a string:
logger.info("barfoo");
This configuration results in the following warnings/errors on launch:
log4j:WARN File option not set for appender [logfile].
log4j:WARN Are you using FileAppender instead of ConsoleAppender?
log4j:ERROR No output stream or file set for the appender named [logfile]
It does not produce the log file. The console logging seems to be working just fine, however.
This will sound stupid - you have a typo. You have log4j.appedner.logfile.File instead of log4j.appender.logfile.File (note the 'n' and 'd' are switched). This is why it's complaining that you haven't set the File.

Trouble configuring log4j

I'm using Java 6 on Win XP and log4j 1.2.12. I'm having trouble getting my logs output to a file (no log4j.log appears). Below is my log4j.properties config, which appears in my war's WEB-INF/classes directory ...
log4j.rootLogger=DEBUG, A2
log4j.appender.A1 = org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
log4j.appender.A2 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.File = c:\apache-tomcat-6.0.33\logs\log4j.log
log4j.appender.A2.Append = true
log4j.appender.A2.DatePattern = '.'yyy-MM-dd
log4j.appender.A2.layout = org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
And here's how I access the logger in my class ...
public class XMLNode {
private static Logger logger = Logger.getLogger(XMLNode.class);
...
logger.debug("test:" + main.toString());
Any other ideas what else I should do or check to get my log4j.properties file to appear? Thanks, - Dave
Your problem appears to be with the format of the file name.
log4j.appender.A2.File = c:\apache-tomcat-6.0.33\logs\log4j.log
You will need to change the file path to use either forward slashes (/) or double back slashes (\\)
log4j.appender.A2.File = c:/apache-tomcat-6.0.33/logs/log4j.log
I ran your example above, and when I changed the file path it worked fine.
use direct LOGGER.info(String-Message).. hope so you will get the log file..
i think your log file is present but you are not searching it at right place.. find it in

log4j:Appender to print the log Statements

Hello sir:
When server is started ,the log files print the output statement..how can i print the log files in my own file?
You can try using a properties file
log4j.rootLogger=INFO, ERRORFILE
#ERRORFILE - used to log error messages
log4j.appender.ERRORFILE=org.apache.log4j.RollingFileAppender
log4j.appender.ERRORFILE.Threshold=ERROR
log4j.appender.ERRORFILE.File=errorLog.log
log4j.appender.ERRORFILE.MaxFileSize=200KB
log4j.appender.ERRORFILE.MaxBackupIndex=5
log4j.appender.ERRORFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ERRORFILE.layout.ConversionPattern=%d{DATE} %t - %m%n
In this example, I configured a properties file to log error messages to my own file, errorLog.log. Here's another sample.
Configure a log4j properties file with file appender. Something like this -
log4j.rootLogger=WARN, fileAppender
log4j.logger.myPackage=DEBUG, fileAppender
log4j.additivity.myPackage=false
log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.fileAppender.File= C://Myfile.log
log4j.appender.fileAppender.MaxFileSize=1024KB
log4j.appender.fileAppender.MaxBackupIndex=5
log4j.appender.fileAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

Categories