Document directory
- Main Interface Introduction
Since the logging component is added to the Apache Jakarta Commons project, logging has become the most common log component for Java development. I think there are two main reasons for the emergence of the logging component:
1. unified interface.
There are many Log service components in the Java Community. log4j, JDK log, logkit, and many other projects provide their own log components, such as aveon. These components do not have a uniform interface and configuration, which makes it more difficult for us to choose which log component to use. The logging component unifies these interfaces, so that we only focus on the log interfaces provided by logging. The specific log components can be flexibly selected.
2. You can flexibly decide whether or not logs are required and what log implementation is used.
Although logj4j and other log components provide powerful functions, the system consumption of these components is also relatively large. These log operations may greatly affect the system performance. Therefore, after the system is launched, many log functions originally used for debugging will no longer be required, or all application logs are not required, or we need our own customized Log service. The logging component first provides empty logs, that is, if this log implementation is used, we can remove all log output without changing the program. Of course, we can also expand logging to implement our own log service and deploy our log service as needed.
Currently, the main logging function in DOTNET is the system log provided by framework and log4net transplanted to Java log4j. Second, DOTNET does not have a component similar to commons logging (at least I don't know ). Therefore, ibatisnet implements a public service similar to commons logging in the common project.
Main Interface Introduction
The log function of ibatisnet is through the adapter
The main interface of ibatisnet's log function isIlogIt defines the main functions of the log service. These functions are the same as those provided by log4net and can be divided into seven levels, which are defined in the public Enum loglevel:
public enum LogLevel
{
All = 0,
Debug = 1,
Info = 2,
Warn = 3,
Error = 4,
Fatal = 5,
Off = 6,
}
Two methods and one read-only bool attribute are defined for each level from 1 to 5, such as the debug level:
void Debug( object message ); |
void Debug( object message, Exception exception ); |
bool IsDebugEnabled { get ; } |
We should be familiar with these issues and will not discuss them much.
Another important interface is public interface.IloggerfactoryadapterIt is an adapter factory. The specific implementation is used to generate the ilog implementation in ibatisnet common that adapts to different log implementations. (Let's take a look at the specific example to get it clear ). Iloggerfactoryadapter is defined as follows:
namespace IBatisNet.Common.Logging
{
public interface ILoggerFactoryAdapter
{
ILog GetLogger( Type type );
ILog GetLogger( string name );
}
}
The implementations currently provided in ibaitsnet include consoleoutloggerfa, nooploggerfa, traceloggerfa, and log4netloggerfa in ibatisnet. Common. Logging. impl. Ilog service for ibatis custom log service is generated for the first three adaptation factories; For log4netloggerfa in ibatisnet. Common. Logging. impl, it is mainly used to generate ilog implementation for log4net Log Service adaptation. Let's take a look at the implementation of leleoutloggerfa.
Public ilog getlogger (type)
{
Return getlogger (type. fullname );
}
Public ilog getlogger (string name)
{
// Slightly modify the cache
Ilog log = new consoleoutlogger (name, _ level, _ showdatetime, _ showlogname,
Return log;
}
We can see that the adaptation factory actually constructs and returns a specific consoleoutlogger class that implements the ilog interface. Of course, the constructor of this class initializes the consoleoutlogger with the passed attribute array. The implementation of eagleoutlogger is as follows:
Public class consoleoutlogger: ilog // implements the ilog Interface
The constructor sets the loglevel and other related parameters bool showdatetime, bool showlogname, string datetimeformat.
public ConsoleOutLogger( string logName, LogLevel logLevel, bool showDateTime, bool showLogName, string dateTimeFormat)
{
_logName = logName;
_currentLogLevel = logLevel;
_showDateTime = showDateTime;
_showLogName = showLogName;
_dateTimeFormat = dateTimeFormat;
if (_dateTimeFormat != null && _dateTimeFormat.Length > 0)
{
_hasDateTimeFormat = true;
}
}
Isdebugenabled determines the debug switch status through the loglevel during construction.
public bool IsDebugEnabled
{
get { return IsLevelEnabled( LogLevel.Debug ); }
}
For debug output
public void Debug(object message)
{
Debug( message, null );
}
public void Debug(object message, Exception e)
{
if ( IsLevelEnabled( LogLevel.Debug ) )
{
Write( LogLevel.Debug, message, e );
}
}
Write Method to Construct log information and output it to system. Console. Out
Private void write (loglevel level, object message, exception E)
In addition, public sealed class nooplogger: ilog is a default log ilog implementation class that does not generate any output.
Ii. Use
The above briefly introduces the main interface of ibatis common logging. Now let's take a look at its usage. Logging is easy to use. The classes or methods that require logs in the program can be implemented in the following way:
1. Get a log implementation class that implements the ilog interface through logmanager. Configure the specific implementation in the configuration file.
Private Static readonly ilog _ logger = logmanager. getlogger (methodbase. getcurrentmethod (). declaringtype );
Or
Private Static readonly ilog _ logger = logmanager. getlogger (typeof (Specific Class Name ));
The first type is recommended.
2. Use the ilog Mode
if (_logger.IsDebugEnabled)
{
_logger.Debug("Dao Proxy call to " + invocation.Method.Name);
}
3. The key to simple logging configuration is configuration. The configuration is handled by configurationsectionhandler. Public class configurationsectionhandler: iconfigurationsectionhandler. The create method is implemented to generate logsetting according to the configuration. Logsetting encapsulates factoryadaptertype and properties. This information is obtained from the configuration.
public Type FactoryAdapterType{ get { return _factoryAdapterType; }}
public NameValueCollection Properties{ get { return _properties; }}
The configuration example in the common test package is as follows:
<configSections>
<sectionGroup name="iBATIS">
<section name="logging" type="IBatisNet.Common.Logging.ConfigurationSectionHandler, IBatisNet.Common" />
</sectionGroup>
</configSections>
The above configuration tells us to use ibatisnet. Common. Logging. configurationsectionhandler for the logging configuration section in the ibatis Configuration group.
<iBATIS>
<logging>
<logFactoryAdapter type="IBatisNet.Common.Logging.Impl.ConsoleOutLoggerFA, IBatisNet.Common">
<arg key="showLogName" value="true" />
<arg key="showDateTime" value="true" />
<arg key="level" value="All" />
<arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:SSS" />
< /logFactoryAdapter>
</logging>
</iBATIS>
The preceding configuration section is used by ibatis commons logging. When we obtain ilog through the getlogger method of logmanger, logmanger first checks whether the preceding logging section has been configured in the configuration file, if no configuration is available, the instance of the consoleoutloggerfa adaptation factory class (builddefalogloggerfactoryadapter () will be constructed, that is, the common logging will use leleoutlogger. Otherwise, consoleoutlogger is also used if the type defined in logfactoryadapter is not implemented by iloggerfactoryadapter. Otherwise, a subclass of the iloggerfactoryadapter implementation class is generated based on the type specified by reflection, and a log component that implements the ilog interface is generated for this class.