Log4j 2.0 advanced usage in development-read configuration file (6) and log4j usage

Source: Internet
Author: User

Log4j 2.0 advanced usage in development-read configuration file (6) and log4j usage
In log4j, the location where log files are stored is not necessarily under src, that is, under the root directory. In this case, we need to solve the problem of loading the configuration file. There are many solutions in log4j1. x. For example, PropertyConfigurator. configure (); and DOMConfigurator. configure. In log4j2. x, the two classes do not exist. How can we load the configuration file at this time? The answer is also very simple, that is, the log4j2. x version provides me with the ConfigurationSource and Configurator classes. We can use them to manually load configuration file information at any location. I will mainly introduce three methods: log4j 2 three methods to read the configuration file. Configuration Files Read by log4j 2 can be divided into three types: configuration files under src, absolute path configuration files, and relative path configuration files. Let's give an example one by one. View the Code directly:

Package com. herman. test; import java. io. file; import java. io. fileInputStream; import java.net. URL; import org. apache. logging. log4j. logManager; import org. apache. logging. log4j. logger; import org. apache. logging. log4j. core. config. configurationSource; import org. apache. logging. log4j. core. config. aggregator; public class ConfigTest {private static Logger logger = LogManager. getLogger (ConfigTest. class);/*** log4j 2 read the configuration file * log4j 2 read the configuration file can be divided into three types: configuration file under src, absolute path configuration file, relative path configuration file * // The first class loads the configuration file under src public static void test0 () {// the configuration file under src will be loaded by the log4j framework by default, and we will not display the loaded // directly test logger.info ("I printed ....... "); // output content //2014-09-01 15:49:30, 229 INFO [main] test. configTest (ConfigTest. java: 18)-I printed .......} // The configuration file public static void test1 () in the second absolute path. put xml in drive D // This is the need to manually load // absolute path configuration file ConfigurationSource source; try {// method 1 use public ConfigurationSource (InputStream stream) throws IOException constructor source = new ConfigurationSource (new FileInputStream ("D: \ log4j2. xml "); // method 2 use the public ConfigurationSource (InputStream stream, File file) constructor File config = new File (" D: \ log4j2. xml "); source = new ConfigurationSource (new FileInputStream (config), config); // method 3 use the public ConfigurationSource (InputStream stream, URL url) constructor String path =" D: \ log4j2. xml "; source = new ConfigurationSource (new FileInputStream (path), new File (path ). toURL (); // source. setFile (new File ("D: \ log4j2. xml "); // source. setInputStream (new FileInputStream ("D: \ log4j2. xml "); aggregator. initialize (null, source); Logger logger = LogManager. getLogger (ConfigTest. class. getName (); logger. trace ("trace... "); logger. debug ("debug... "); logger.info (" info... "); logger. warn ("warn... "); logger. error ("error... "); logger. fatal ("fatal... "); // run the following command:/* 16:03:07, 331 DEBUG [main] test. configTest (ConfigTest. java: 42)-debug... 2014-09-01 16:03:07, 331 INFO [main] test. configTest (ConfigTest. java: 43)-info... 16:03:07, 331 WARN [main] test. configTest (ConfigTest. java: 44)-warn... 16:03:07, 331 ERROR [main] test. configTest (ConfigTest. java: 45)-error... 16:03:07, 331 FATAL [main] test. configTest (ConfigTest. java: 46)-fatal... */} catch (Exception e) {e. printStackTrace () ;}// the configuration file of the Third-class relative path loads public static void test2 () {// note that Chinese characters and spaces are not allowed in the path, please use url transcoding ConfigurationSource source; try {// method 1 use getResource () String path = "/com/herman/config/log4j2. xml "; URL url = ConfigTest. class. getResource (path); source = new ConfigurationSource (new FileInputStream (new File (url. getPath (), url); aggregator. initialize (null, source); // method 2 using System. getPropertyString config = System. getProperty ("user. dir "); source = new ConfigurationSource (new FileInputStream (config +" \ src \ com \ herman \ config \ log4j2. xml "); aggregator. initialize (null, source); // output content/* 16:32:19, 746 DEBUG [main] test. configTest (ConfigTest. java: 53)-debug... 2014-09-01 16:32:19, 746 INFO [main] test. configTest (ConfigTest. java: 54)-info... 16:32:19, 746 WARN [main] test. configTest (ConfigTest. java: 55)-warn... 16:32:19, 746 ERROR [main] test. configTest (ConfigTest. java: 56)-error... 16:32:19, 746 FATAL [main] test. configTest (ConfigTest. java: 57)-fatal... */} catch (Exception e) {e. printStackTrace () ;}} public static void main (String [] args) {// test0 (); // test1 (); test2 ();}}
Easy! Welcome to follow my blog !!!! If you have any questions or problems, add the QQ group: 135430763 for feedback and learn together!
How to obtain the log4j configuration file

?? 1 .???????????????? The configuration file is loaded for the first time. Therefore, the path of the configuration file to be loaded must be specified when the system project uses log4j for the first time ?? 2 .???????????????? On a linux server, the relative path is used. How can I specify the configuration file ?? Method 1: // specify the configuration file's loading directory to load the. properties file PropertyConfigurator. configure ("E: // log4j. properties ");?? Method 2 :/**?????? ?? * Initialize log4j configuration information ?????? ?? * @ ThrowsException ?????? ?? */?????? Publicstaticvoid initLog4j () throws Exception ?????? {???????????? FileInputStream istream = null ;???????????? Try ???????????? {???????????????????? Properties props = new Properties (); // specifies the path for obtaining the configuration file ???????????????????? Istream = new FileInputStream ("conf/log4j. properties ");???????????????????? Props. load (istream); // read the attribute list ???????????????????? PropertyConfigurator. configure (props );???????????? } Catch (Exception ex )???????????? {???????????????????? Log. error ("initLog4j. error:", ex );???????????????????? Thrownew Exception (ex );???????????? } Finally ???????????? {???????????????????? Istream. close ();???????????? }?????? }?? Using the above method, we can dynamically add configuration information. How can we dynamically specify the Log storage path, that is, there is no need to write it to death?

How do I read the log4jproperties configuration file?

The question is wrong. The question should be answered in the JAVA section.
 

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.