Log operations with Logkit
1 Overview
In any system, the log is indispensable. Now Apache provides two sets of log tools, one is log4j, and the other is a logkit to give an example of this article.
Log4j and logkit have a lot of similar places. For example, log4j provides a 5-level log: Debug, Info, Warn, Error and Fatal, Logkit also offers 5-level log: Debug, Info, Warn, Error and Fatal-Error, in addition to the naming of the level 5, essence is the same.
Logkit also provides directory feature, and controlling the log format, using Layout in log4j, and in logkit is formatter. For log output, log4j uses appender, logkit uses a more direct name: Target.
This article is of course not used to compare the difference between Logkit and Log4j, but to explain why Logkit needs to be used after a log tool with log4j.
The reason for using Logkit is: Context and LogTargets. When using log4j, the content of the log can only be a sentence, and use logkit, you can record many items, and even record in the corresponding database field. If you use the log4j storage log to a different storage medium, such as a database, you need to use the Appender, and Logkit can support multiple storage targets.
The following program will use a product detection line as a demonstration.
2. An example
The product detection line is used to check if the product is qualified, requiring the product number, whether the product is detected, briefly explained three projects. Logkit will also record the level, time, and information as a reference.
Establish a Logkitexample table on a free mysql database: SQL statement:
Create Table Logkigexample
(
LogMessage Varcher (1000),
Logpriority varchar (20),
Logtime DateTime,
ProductNumber Varchar (100),
ProductPass Varchar (10),
ProductExplain Varchar (100)
)
In this product detection line, whether the product is divided into three situations: OK, SOSO and BAD. Among them, OK represents product quality, SOSO represents the quality, and the BAD representative does not pass inspection and needs to be re-manufactured.
code show as below:
Package logkitexample;
Import java.lang. *;
Import java.util. *;
Import org.apache.log. *;
Import org.apache.log.output.db. *;
Public Class ProductChecker, PUBLIC CLASS
{
/ ** ProductChecker Loger * /
Static private org.apache.log.logger loggerproductchecker;
Public static void main (String [] Argv)
{
ProductChecker_P = new productChecker ();
_P.initLogkit ();
// Simulate product check results
Random_Checkrandom = New Random ();
INT _CHECKRESULT;
For (INT i = 1; i <= 20; i ) {
_CHECKRESULT = (int) (_ checkrandom.nextfloat () * 3);
Switch (_CHECKRESULT)
{
Case 0:
ContextMap.bind (_p.getProductCheckermap ("NO." String.Valueof (i), "OK", "OK"));
LoggerProductChecker.info ("ProductChecker Pass");
Break;
Case 1:
ContextMap.bind (_p.getProductCheckermap ("NO." String.Valueof (i), "SoSO", "Check Again));
LoggerProductChecker.warn ("ProductChecker no good";
Break;
Case 2:
ContextMap.bind (_P.GetProductCheckermap ("NO." String.Valueof (i), "Bad", "REDO"));
LoggerProductChecker.Error ("ProductChecker Bad");
Break;
}
}
}
/ ** Initialize logkit * /
Private vidinitlogkit ()
{
Try
{
// Register the data source used
Class.Forname ("org.gjt.mm.mysql.driver");
DefaultDataSource _DataSource = New DefaultDataSource ("JDBC: MySQL: // LocalHost / Logkitexample", "root", "");
// Register the corresponding column mapping relationship
ColumnInfo [] _COLUMEPRODUCTCHECKER = {
New ColumnInfo ("LogMessage", ColumnType.Message, Null,
New ColumnInfo ("LogPriority", ColumnType.Priority, NULL,
New ColumnInfo ("Logtime", ColumnType.Time, NULL,
New ColumnInfo ("ProductNumber", ColumnType.Context, "ProductNumber",
New ColumnInfo ("Productpass", ColumnType.Context, "ProductPass",
New ColumnInfo ("ProductExplain", ColumnType.Context, "ProductExplain",
}
// Register JDBCTARGET
DefaultjdbCTarget_targetProductChecker =
New defaultjdbctarget (_datasource, "logkitexample", _COLUMEPRODUCTCHECKER;
// Level of registration logs
Org.apache.log.hierarchy _hierarchy = new org.apache.log.hierarchy ();
LoggerProductChecker = _Hierarchy.getLoggerFor ("logkitexample"); // Set the targetChecker logger used Target
LoggerProductChecker.SetLogTargets
New logtarget [] {_targetProductChecker};
/ / Set the log level for debug
LoggerProductChecker.SetPriority (org.apache.log.priority.debug);
}
Catch (Exception E)
{
System.out.println ("Logkitinit Error");
}
}
/ ** Get product logs ContextMap * /
Private org.apache.log.contextMap getProductCheckermap (string _productNumber, string _productpass, string _productexplain)
{
Org.apache.log.contextmap _cm = new org.apache.log.contextMap ();
_cm.set ("ProductNumber", _ProductNumber);
_cm.set ("ProductPass", _ProductPass;
_cm.set ("ProductexPlain", _ProductExplain;
Return (_CM);
}
}
3. Logkit's storage target
Logkit supports a variety of different log storage targets, called LogTargets, including files, databases, IRC channels, JMS, or even any Sockets definitions.
One logger in Logkit can correspond to different logTargets, using Filter to record different log levels to different log levels. For example, the log is stored in the morning database, but Fatal_Error is to be placed in a text file, because in this case, it is very likely that the database is unavailable.
Logkit also supports asynchronous LogTargets for logTARGETs that cannot respond real-time, such as mail systems, etc.
4. Reference
Logkit Project Home: http://jakarta.apache.org/avalon/logkit/index.html