Use log4j to join the log in the program Select BLOG from LDGS

xiaoxiao2021-03-06  26

Learn the log4j online, the following is some simple learning note Apache's open source project log4j is a powerful log component, providing fast and effective logging, developers can add very much to their own programs. Practical log function. In Apache website: http://jakarta.apache.org/log4j You can download the latest version of the LOG4J for free, decompressed, copied the Log4j-1.x.x.jar files that packaged into the ClassPath-related directory. First, quickly experience OK, now you can experience the functionality of log4j, follow these steps to quickly join the log functionality in the program. See the simple example below: Step 1: Import Log4j Use of IMPORT Related class second step: Define A log logger logger's object Step 3: Load log4j configuration file, or use default Configuration of Environment Step 4: You can now want to output a log in your program, using the following methods provided by the Logger class, insert different priority logs. Debug (Object Message); Info (Object Message); Object Message; Error (Object Message); Datunate! Is it very simple? It can fully replace the statement such as system.out.println ("xxxxxxxxxxxx" previously used to debug the program, and log4j can flexibly control the level of log output by custom profile.

############################################################################################################################################################################################################################################ ** Import Log4j Related Class * / Import Org.Apache.log4j.logger; import org.apache.log4j.basicconfigurator; / ** *

Title: log4j example *

Description : Quick use log4j *

copyright: Copyright (c) in the program, 2004 * @Author robin * @version 1.0 * / public class hellolog4j {// Define logging logger logger, all The log is listed by it, static logger logger = logger.getlogger (Hellolog 4j.class.getClass ()); public static void main (String [] args) {// uses the default environment to configure log4j BasicConfigurator.configure (); // Use the configuration file to configure log4j //propertyConfigurator.configure ("D: //testlog4j.properties); // Insert log information logger.info (" Hellolog4j start "); try {hellog4j hellolog4j1 = new hellolog4j () INT count = hellog4j1.count (1, -32); logger.debug ("The value of count is:" count);} catch (exception e) {logger.error ("error" E.GetMessage ()) } Logger.info ("HelloG4J execution");} / ** * Since the sum of two intensity * * / public int count (int A, int b) {int Add = a b; if (Add <0 ) {Lo Gger.warn ("Two Dixes Since and less than zero");} Return Add;}} ################################################################################################# ####

####### 2, Configuring Log4j We can dynamically control the output of the log by custom definition profile. Log4j supports two configuration file formats, one is a file in XML format, one is a Java feature file (key = value). You can load the configuration file in the following ways: 1, load the configuration file written by using the Java key-value-paid properties file. PropertyConfigurator.configure (String ConfigfileName) 2, loaded into the configuration file Domconfigurator.configure (String FileName).

Look at a simple Java property profile testlog4j.properties :##################################################################################################################################################################################################################################################################################### #### Specify root logger, and log output level, greater than or equal to this level of log will be output (Debug

Divided into all

转载请注明原文地址:https://www.9cbs.com/read-65139.html

New Post(0)