- ConsoleAppender - Append to the console
- FileAppender - Append to the log file
- SMTPAppender - Logs through emails
- RollingFileAppender - Logs to files, when it comes to defined limit go for a new file.
There are many more types can be used,
AsyncAppender, JDBCAppender, JMSAppender, LF5Appender, NTEventLogAppender,
NullAppender, NullAppender, SMTPAppender, SocketAppender, SocketHubAppender,
SyslogAppender, TelnetAppender, DailyRollingFileAppender, RollingFileAppender.
To begin with log4j, you should first download the log4j package. Here is a sample link to download it.
Log Levels
There are some log levels defined in log4j.- all - All levels including custom levels
- trace (since log4j 1.2.12) - Developing only, can be used to follow the program execution.
- debug - Developing only, for debugging purpose
- info - Production optionally, Course grained (rarely written information), I use it to print that a configuration is initialized, a long running import job is starting and ending.
- warn - Production, simple application error or unexpected behaviour. Application can continue. I warn for example in case of bad logging attempts, unexpected data during import jobs
- error - Production, application error/exception but
application can continue. Part of the application is probably not working - fatal - Production, fatal application error/exception, application cannot continue, for example database is down.
- no - Do not log at all.
A simple example
First you better add downloaded jar package to the project using your IDE. Then you have to setup the log4j properties through property file or, xml file.... In this case, let's check with log4j.properties file. In your IDE, as other property files add your log4j properties file. For example create new file called "log4j.properties" It should be look like this.log4j.rootLogger=debug, fileI am using RollingFileAppender here. Max file size is 100KB, kind of all the properties related to Log4j is stated here.
log4j.appender.file=org.apache.log4j.RollingFileAppender #logging Type
log4j.appender.file.maxFileSize=100KB #Max file size of a log file
log4j.appender.file.maxBackupIndex=5 #keeping backups of
log4j.appender.file.File=C://myprogram_logs/test.log #Location which saving log file
log4j.appender.file.threshold=debug #Log Level
log4j.appender.file.layout=org.apache.log4j.PatternLayout #Layout of the log file
log4j.appender.file.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n #Layout of the log file
At your java file, load the property file and put it into the PropertyConfigurator on log4j package. Example code snipped here,
Improte packages
On the relevant class, define the Logger type global variable(in my case "logger").
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;
import java.util.Properties;
private static Logger logger = Logger.getLogger(MyProgram.class);On main method,
public static void main(String[] args){When you catch an exception, write it on log as follows
Properties properties = new Properties();
InputStream inputStream = MyProgram.class.getClassLoader().getResourceAsStream("log4j.properties");
properties.load(inputStream);
PropertyConfigurator.configure(properties);
logger.info("My program Begins");
}
try {
//Your code here
} catch (Exception e) {
logger.error("Found the exception here");
}
Now you will find logs on C://myprogram_logs/test.log location file.
When you need to use it in ubuntu, you just have to change the file location only from the properties file (i.e. /var/myprogram_logs/test.log).
See how easy logging you programming status with Log4j........
Here is a nice pdf to follow.
Cheers......
No comments:
Post a Comment