Logger. getLogger and LogFactory. getLog, logger. getlogger
Logger. getLogger and LogFactory. getLog
Logger. getLoggerLogFactory. getLogLogger comes from the log4j package. If you use Logger. getLogger, you need a log4j jar package. In this way, you can only rely on log4j:LogFactory comes from the common-logging packag
In Kafka, logmanager manages all the logs on the broker (each topic-partition is a log ). By reading the source code, we can see that the specific functions are as follows:
1. Clear the Message Queue according to the preset rules.
2. Perform the flush operation on the Message Queue according to the preset rules ).
3. Connect zookeeper to perform zookeeper operations related to broker, topic, and partition.
4. Manage all the logs on the broker.
The fol
1. If you runProgramError:"Exception in thread "Main" Java. Lang. noclassdeffounderror: ORG/slf4j/loggerfactory", This is because the project lacks both the slf4j-api.jar and slf4j-log4j12.jar jar packages.
2. If an error occurs in the running program:"Java. Lang. noclassdeffounderror: ORG/Apache/log4j/logmanager", This is because the project lacks the jar package log4j. jar.
3. Error:"Exception in thread "Main" Java. Lang. nosuchmethoderror: org.
logger log output, the log is only processed once, with its own handler output, will not be passed to the parent logger handler). The following is an example of a JDK configuration file
handlers= java.util.logging.filehandler,java.util.logging.consolehandler
. level= INFO
Java.util.logging.FileHandler.pattern =%h/java%u.log
java.util.logging.FileHandler.limit = 50000
Java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = Java.util.logging.XMLFormatter
java.util
Sun developed a logger in jdk1.4, and related classes are organized in the Java. util. Logging package. The most important is the logger class. Its instances can help us record log information.
We often use the following two steps to record log information:
Logger log = logger. getlogger ("log name ");
Log.info ("information to be recorded ");
So, what is behind this simple two-step operation?
Let's take a look at the implementation of the logger.
console level, but can only change the log level output to the file.
2. Modify logging. properties
The default external configuration file is the lib/logging. properties file in JRE. You can open this file and modify the following two actions:
. Level = ALL//...Java. util. logging. lelehandler. level = ALL
This method affects all jre users.
In order not to affect all users, we can also read the specified configuration file through the readConfiguration (InputStream ins) of
, eventargs E)
{
Ilog logger = logmanager. getlogger ("test ");
Logger. debug ("test information ");
}
}
Finally, you can find the log file in the root directory.
Source code of this example: Download the source code of this Article
Note: For more information about this article, see the blog. I use vs2008, but the class library is. Net framework2.0.
Note: It takes 10 minutes for an instance to complete.
, the process is like this. First, we need to create an ILoggerRepository to configure the Log Level and various Appender, then, you can use LogManager to obtain an Ilog object in ILoggerRepository to write logs. The Code is as follows:
Public static ILog GetLogger (string repositoryName = "") {if (string. isNullOrEmpty (repositoryName) return LogManager.
If we need to configure different ilogs, the process is like this. First, we need to create an ILoggerRepository to configure the Log Level and various Appender, then, you can use LogManager to obtain an Ilog object in ILoggerRepository to write logs. The Code is as follows:
Copy codeThe Code is as follows:Public static ILog GetLogger (string repositoryName = ""){If (string. IsNullOrEmpty (repositoryName) r
inherits the attributes of the ancestor. The log4net Framework defines an ilog interface, which must be implemented by all logger classes. If you want to implement a custom logger, you must first implement this interface. You can refer to several examples in the/extension directory. The ilog interface is defined as follows: Public interface ilog { Void debug (Object message ); Void Info (Object message ); Void warn (Object message ); Void error (Object message ); Void fatal (Object mess
For a product or project, a detailed log is usually printed during testing. After the product is released, the highest level of log4j is usually set to Improve the efficiency, once a problem occurs to the customer, it is important to check the detailed log information to confirm the log printing. However, it is not good to let the customer manually modify the log4j configuration file.Modify the log4j levelPublic class log4jconfig {
Public void enableinfo (string target ){
Technorati label: log4net configuration, independent log4net. config, non-embedded
Example 1:
Vs2003The created Asp.net project, the log4net file is placed in the web. config file
1. Add a reference to log4net. dll (download DLL)
2. Add in Web. config (Note the location of the peer, under the
3. Page_load in webform1.aspx. CS
Log4net. ilog logger = log4net. logmanager. getlogger (system
not provide the log4j configuration file in the project, log4j uses the default configuration, the method for obtaining the Logger in error * 4 */private static logger Logger = LogManager. getLogger (LogManager. ROOT_LOGGER_NAME); private static Logger log = LogManager. getLogger
. getcurrentclasslogger (). We can see that it is implemented as follows:
Logmanager. getcurrentclasslogger// Nlog. logmanagerPublic StaticLogger getcurrentclasslogger () {stackframe =NewStackframe (1,False);ReturnLogmanager. getlogger (stackframe. getmethod (). declaringtype. fullname );}
The code is very simple, that is, the stack technology is used to obtain the caller's class name of a method, and
Step 1:
Add and apply Log4net. dll. Add the following configuration board to the Web. config file.
Copy codeThe Code is as follows:
Step 2:
Create a Log4Net. config configuration file and add the following configuration information:
Copy codeThe Code is as follows:
Step 3:
Add the following code to the Application_Start event in the Global. asax file:
Copy codeThe Code is as follows: protected void Application_Start (object sender, EventArgs e)
{
// Code that runs on application s
console appender, and all logs that are created by the log instance are printed according to this appender output.You only need to call the default initialization before you create the log instance:
static {
basicconfigurator.configure ();
Logger = Logmanager.getlogger (Log4jonetest.class);
The console output format is as follows and does not output trace level logs:
the Convention for the configuration file path:Log4j the location of the configuration file in the Classpath, without s
the log level later)Bool isdebugenabled;Bool isinfoenabled;//... Boolean attribute corresponding to other methods}
The log4net Framework defines a class called logmanager to manage all logger objects. It has a static getlogger () method that uses the name parameters we provide to retrieve existing logger objects. If this logger object does not exist in the framework, it will also create a logger object f
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 );OrPri
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.