Use log4j.jar in my java file. - java

I have class file like this, using log4j.jar is easy in eclips IDE. but not through windows command prompt.
import log4j.Logger;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
public class log4jExample{
/* Get actual class name to be printed on */
static Logger log = Logger.getLogger( log4jExample.class.getName());
public static void main(String[] args)
throws IOException,SQLException {
log.debug("Hello this is an debug message");
log.info("Hello this is an info message");
}
}
and I create log4j properties like this:
# Define the root logger with appender file
log = /usr/home/log4j
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
what should I do to get log records in cmd and file

Just define a ConsoleAppender and the log messages will go both to the file and to the console.
http://logging.apache.org/log4j/1.2/manual.html
Your config file should be
# Define the root logger with appender file
log = /usr/home/log4j
log4j.rootLogger = DEBUG, FILE, A1
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Related

Log4j doesn't create audit log file

I want to track audit logs to separate file audit.log
I have the following configuration for log4j
# Root logger option
log4j.rootLogger=INFO, stdout
# 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
# Audit logs
log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=/home/michal/audit.log
log4j.appender.A2.Append=true
log4j.appender.A2.encoding=UTF-8
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} - AUDIT - %m%n
In my code I'm calling
private static final Logger logger = Logger.getLogger("A2");
logger.info("audit test");
But audit log file is not created. When I put A2 level to rootLogger, the file is created but it is full of logs from stdout target.
A2 is an appender not a logger. You need to add a line to your log4j configuration to define a logger that writes to your appender and then you can write to that in your java code.
In your config
log4j.logger.audit=INFO, A2
and in your java code
private static final Logger logger = Logger.getLogger("audit");

Issue with log4j log not writing to file

Does anyone see why this log is not writing to file. It is writing to standard out twice but not to the file:
Also, I tried removing the "Stdout" appender and then I don't get any logging at all.
package org.berlin.wicket;
import org.apache.log4j.Logger;
private static final Logger LOG = Logger.getLogger(QuickstartPage.class);
LOG.info("Loading constructor");
log4j.rootLogger=DEBUG,Stdout,mainAppender
log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} -
%m\n
log4j.appender.mainAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.mainAppender.file=logs/core.log
log4j.appender.mainAppender.datePattern='.'yyyy-MM-dd
log4j.appender.mainAppender.append=true
log4j.appender.mainAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.mainAppender.layout.ConversionPattern=[%d{MM/dd/yyyy
HH:mm:ss.SSS}] [%C{1}.%M():%L] [%t] [%p] - %m%n
log4j.logger.org.berlin=DEBUG,Stdout,mainAppender
You have two loggers which use the same appender (Stdout), hence why you see entries on the console twice.
As others have mentioned, the properties are case-sensitive, hence why your file appender is not configured correctly.
The File and Append properties are case senstive.
log4j.appender.mainAppender.File=someFileName.log
log4j.appender.mainAppender.Append=true
You should create an instance of FileHandler that writes log to a file called myfile.log.
FileHandler fileHandler = new FileHandler("myfile.log", true);
logger.addHandler(fileHandler);

Log4J loggers for different classes

I want to use Log4J for logging my java projects.
I created a log4j.properties file in the src directory with the following content:
# Root logger option
log4j.rootLogger=INFO, file, stdout
log4j.logger.DEFAULT_LOGGER=INFO,file2
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=file.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p %c - %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{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.file2=org.apache.log4j.FileAppender
log4j.appender.file2.File=file2.log
log4j.appender.file2.layout=org.apache.log4j.PatternLayout
log4j.appender.file2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
For example I want to use the "DEFAULT_LOGGER" only in my main method. So I wrote:
static Logger log = Logger.getLogger("DEFAULT_LOGGER");
log.fatal("Process Logger");
But when I execute the main method I prints the message "Process Logger" to all Appenders (stdout, file and file2) But I only want to print it out to file2. How can I do that or better ro say what do I do wrong?
The second point is, when I execute the main method the second time, it does not overwrite file and file2, it justs adds a line inside the textfile. How can I avoid that?
Log4j has something called additivity. by default it is set to true and it means that every log you write will not only be logged by the specific logger but also by its ancestors (the root logger in this case).
To set it to false try this:
log4j.additivity.DEFAULT_LOGGER = false
Learn more about it here.
try:
log4j.additivity.DEFAULT_LOGGER = false

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

configure log4j to log to custom file at runtime

Can anyone please guide me as to how I can configure log4j to log to a specific file that I specify at run-time.The name and path of the log file are generated at run-time and the application must log to that particular file.
Generally file appender entries in the log4j.properties file points to the log file that will be used by the application.However in this case I would like to read the log file path from the command line and log to that particular file.
How can I achieve this ?
You can also do this from the log4j.properties file. Using the sample file below I have added the system property ${logfile.name}:
# logfile is set to be a RollingFileAppender
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${logfile.name}
log4j.appender.logfile.MaxFileSize=10MB
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%-5p]%d{yyyyMMdd#HH\:mm\:ss,SSS}\:%c - %m%n
The log file name can then be set two different ways:
As a command line, system property passed to java "-Dlogfile.name={logfile}"
In the java program directly by setting a system property (BEFORE you make any calls to log4j).
System.setProperty("logfile.name","some path/logfile name string");
Adapted from the log4j documentation:
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;
public class SimpandFile {
static Logger logger = Logger.getLogger(SimpandFile.class);
public static void main(String args[]) {
// setting up a FileAppender dynamically...
SimpleLayout layout = new SimpleLayout();
FileAppender appender = new FileAppender(layout,"your filename",false);
logger.addAppender(appender);
logger.setLevel((Level) Level.DEBUG);
logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}
Can be also done by this properties define in log4j.properties file
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.maxFileSize=5000KB
log4j.appender.logfile.maxBackupIndex=5
log4j.appender.logfile.File=/WebSphere/AppServer/profiles/Custom01/error.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p [%t] %C %M %c{1}:%L - %m%n
Working and same has been tested
// setting up a FileAppender dynamically...
SimpleLayout layout = new SimpleLayout();
RollingFileAppender appender = new RollingFileAppender(layout,"file-name_with_location",true);
appender.setMaxFileSize("20MB");
logger.addAppender(appender);
Just remove your log4j code and replace this code in your java class it's working good no need to add properties or xml file
SimpleDateFormat dateFormatsd = new SimpleDateFormat("dd-MM-yyyy-hh-mm");
String dateandtime=dateFormatsd.format(new Date());
SimpleLayout layout = new SimpleLayout();
RollingFileAppender appender = new RollingFileAppender(layout,".\\Logs\\log "+dateandtime+".txt",true);
appender.setMaxFileSize("20MB");
logger.addAppender(appender);
The above code will log the logs for every minute.
Thanks

Categories