Let System.out.Println far farm

xiaoxiao2021-03-06  51

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? Dooduoofish is what it is to say is "Why do you go, don't let him manage it? Think about it, we use system.out.println (called SOP, not service oriented programming, is system.out.println) mainly in three cases: 1. Output text to stdout, as output results; Second, display debugging information Used to debug; three, display information, let the administrator watch the first situation, did not say, is the old bank of SOP, not accepting 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 not fresh, 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 output debug information logger.info is the output prompt message logger.warn is to display the warning message logger.Error is displaying the error message log is what is a very deepest ancient Greek noun, not derument, nor derument. 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 are the contents of log4j.properties: #### Use one 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.convender.stdout.Layout.conversionPattern =% 5P (% F:% L) -% M% N is, I want to display all priority equal to and above Debug Information, so the Class is displayed 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 the past half a day, and finally output this thing? Yes. 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 building. XML ANT file,

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

New Post(0)