Let System.out.Println far farm

zhaozj2021-02-16  48

Let System.out.Println far farm, in other words, what should you do?

You may think: system.out.println is almost a few lines in each Java program, how to make his elderly home to farm? How can we have less important revolutionary comrades?

Doodoofish is what you want to say "Why do you go, don't let him manage? Think about it, we use system.out.println (called SOP, not service oriented programming, system.out.println) mainly in three cases:

First, output the text to stdout, as the output result;

Second, show debugging information, used to debug;

Third, display information, let the administrator watch

In the first case, I didn't say that it is the old line of SOP, I can't afford it. Let him continue.

Second, three situations, there is no SOP tube, let him manage it well. If I commissioned to display information with SOP, once I decided not to show these annoying things, what should I do? Well, a file is found, one line is looking, put the SOP to Comment, it is not finished, more convenient what. But I have to show these debugging information again. What should I do? I don't want it next day, I have to go again next month ...? Our programmer is idle? Can someone thinks us Very idle, your boss may be one. My friend 5bug is a genius, he said that there is a way, it is to add an IF before each SOP to determine whether it is displayed. Genius!

I am more stupid, more stupid than Guo Jing. I didn't expect this way, I have never thought of it until 10 weeks, I saw an article introducing Log4j. I don't understand (I am more stupid) at the beginning, I will understand it.

It's a class library, in http://logging.apache.org/site/binindex.cgi downloads. Then, the log4j1.2.2.2.2.jar is then copy Copy to my project logtest from the "Dist / Lib" directory (I am very annoying, the Copy file is an annoying operation, requiring a certain technology). Then, I wrote a simple class (complex I won't write) to call the log4j's stuff.

Import org.apache.log4j. *;

Public class logtest {

Static logger logger = logger.getlogger (logtest.class.getname ());

Public static void main (String [] args) {

Logger.debug ("debug ...");

Logger.info ("Info ...");

Logger.warn ("Warn ...");

Logger. Error ("Error ...");

}

}

Nothing is fresh, it is a logger. That "static logger logger = logger.getlogger (logtest.class.getname ());" Just create a logger object that belongs to the LogTest class, to tell Logger What is your current class, so there is "logtest.class .getname () "This strange Dongdong.

Logger.debug is the information of DEBUG

Logger.info is the output prompt information

Logger.warn is to display a warning message

Logger.Error is the display error message log? log is a very deep ancient greek noun, not derument, nor is it. It's simple, it is "log", what you want to write, where to write, where is it. Different from SOP, log4j allows you to divide what you want to write into level 4 (there are more levels, you can define themselves, like: three-level ), these levels are priority, here four priorities The right is Debug, Info, Warn, and Error, priority from low to high, log4j allows you to control which priority information. Log4j also let you write, you can be a screen, can be a file, or even an email, an XML, a socket, and more. These controls are in a small file, called log4j.properties (actually you can use other names, here is the default). This file is in the project directory. The following is the content of Log4j.Properties:

#### User appender to log to console

Log4j.rootcategory = debug, stdout

#### appender Writes to Console

Log4j.appender.stdout = org.apache.log4j.consoleAppender

Log4j.Appender.stdout.Layout = Org.apache.log4j.patternlayout

Log4j.Appender.stdout.Layout.conversionPattern =% 5P (% F:% L) -% M% N

That is to say, I want to display all the priority equal to and higher than Debug, so the Class is debug ..., info ..., warn ..., error ... all information.

"stdout" means that I defined an output, called STDOUT (any name is good).

The following three lines said that this stdout output is actually the standard output console, which is the screen. The output format is Pattern. The conversion method is% 5P (% F:% L) -% m% N, which is used to display priority, then display the current file name, plus the current number of lines. Finally, logger.debug () or logger.info () or information in logger.warn () or logger.Error (). % N is a carriage return.

Run the program, the last output is:

Debug (logtest.java: 9) - debug ... info (logtest.java: 10) - info ... Warn (Logtest.java: 11) - Warn ... Error (Logtest.java: 12) - Error. .. I have been in half a day, and finally output this thing? Some people say it, "It's better not as SYSTEM.OUT.PRINTLN." But you look, what have you done. First, download the log4j, the Copy class library to the lib directory of the project, write a log4j.properties file (4 lines) three, add "static logger logger = logger.getlogger (logtest.class.getname ()) in the class. "One sentence four, with logger.xxx to output information. Finished. Some benefits you get are: First, change "log4j.rootcategory = debug, stdout" in log4j.properties file to "log4j.rootcategory = OFF, stdout", so all log information will not be displayed; In the log4j.roperties file, "log4j.rootcategory = debug, stdout" is changed into "log4j.rootcategory = info, stdout", which only displays the log information of INFO, WARN, ERROR, and debug information will not be displayed; three, Remote "log4j.rootcategory = debug, stdout" in the log4j.properties file, and stdout, r ", plus the following three sentences: log4j.Appender.r = org.apache.log4j. RollingFileAppender log4j.appender.R.File = log.txt log4j.appender.R.MaxFileSize = 100KB log4j.appender.R.MaxBackupIndex = 1 log4j.appender.R.layout = org.apache.log4j.PatternLayout log4j.appender.R .keout.conversionPattern =% D {YYYY MMM DD HH: mm: SS}% -5p% C -% M% N This, your log information is not only displayed on the screen, and will be saved in a called "log.txt "In the file, the file is up to 100KB. If the file size exceeds 100KB, the file will be backed up into "log.txt.1", the new "log.txt" continues to log information. You can change log4j.properties, without recompiling if you can control whether log information is displayed, log information output type, output method, output format, and so on. Do you have so much benefit from the four walks of work? Do you want system.out.println to display log information? No need. The following is a directory structure of my logtest project: Build.xml file content:

A total of four files, a LogTest class for testing, a build.xml Ant file, a log4j.properties formulated file, a log4j-1.2.8.jar class library. Log4j's function is there, and Doodoofish is not detailed. The following is the reference: don't use system.out.println! Use log4j by Vipan Singla Build flexible log4j by Vikram Goyal log4j by Ashley Js Mills, University of Birmingham add logging to your java applications by Kevin Brown How does The Java logging API Stack UP Against log4j? By Kevin Brown OpenSymphony Logging Primerlog4j FAQ At JGuru

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

New Post(0)