Let system. Out. println go home to farm

Source: Internet
Author: User

Let system. Out. println go home to farm. In other words, what should we do.

You may be wondering: system. Out. println has several lines in almost every Java program. How can we let the old man go home to farm? How can we lose such an important revolutionary comrade?

Doodoofish: "What should I do?", not system. Out. println. Think about it. We use system. Out. println (SOP, not service oriented programming, but system. Out. println) in three scenarios:

1. output text to stdout as the output result;

2. Display debugging information for debugging;

3. display information for Administrators

In the first case, I have to say that it is an old sop. Let him continue.

In the second and third cases, the SOP should not be taken care of, so that he can take care of it. What should I do if I use SOP for debugging to display information? It's easy to find one file, one file, one row, and one line. It's easy to give sop to the comment. But what should I do if I want to display the debugging information again? I don't want to do it again the next day. Will our programmers be idle next month? Some people think we are idle, and your boss may be one. My friend 5 bug is a genius. He said there is a way, that is, adding an if before each SOP to determine whether to display. Genius!

I am dumb and dumb than Guo Jing. I did not think of this method. I never thought of it until I saw an article about log4j in the last 10 weeks. I couldn't understand it at first (I was stupid). I finally understood it again.

A class library is downloaded at http://logging.apache.org/site/binindex.cgi. Then, set the log4j1.2.8. jar files are copied from the "Dist/lib" directory to the "lib" Directory of my project logtest (it is annoying to copy files and requires some technology ). Then, I wrote a simple class (complex, I won't write it) to call log4j stuff.

Import

Org. Apache. log4j .*;

Public

ClassLogtest {

StaticLogger logger = logger. getlogger (logtest.Class. Getname ());

Public Static VoidMain (string [] ARGs ){

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

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

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

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

}

}

Nothing new, just a logger. That"StaticLogger logger = logger. getlogger (logtest.Class. Getname (); "is to create a logger object belonging to the logtest class. When creating a logger, you must inform Logger of what your current class is, so" logtest.Class. Getname () "This strange stuff.

Logger. debug is the output debug information.

Logger.info indicates the output prompt.

Logger. Warn is used to display warning information.

Logger. Error indicates that the error message is displayed.

What is log? Log is a very profound ancient greek term. It is neither derogatory nor derogatory. Simply put, it is "log", that is, what you want to write, where you want to write. Different from SOP, log4j allows you to divide the content to be written into four levels (in fact, you can define more levels, such as Level 3 ++ ), these levels are called priority. Here, the four priorities are debug, info, warn, and error. The priority ranges from low to high. log4j allows you to control the priority information displayed. Log4j also allows you to write anything, such as a screen, a file, an email, an XML, a socket, and so on. These controls are all in a small file called log4j. properties (you can use another name, which is the default name here ). This file is under the project directory. The content of log4j. properties is as follows:

#### Use one appender to log to console

Log4j. rootcategory = debug, stdout

#### Appender writes to console

Log4j. appender. stdout = org. Apache. log4j. leleappender

Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout

Log4j. appender. stdout. layout. conversionpattern = % 5 p (% F: % L)-% m % N

That is to say, I want to display all priority information above and above debug, so the above class will display debug ..., info ..., warn ..., error... all information.

"Stdout" indicates that I have defined an output end called stdout (whatever name is good ).

In the following three lines, the stdout output end is actually the standard output console, that is, the screen. The output format is pattern. The conversion method is % 5 p (% F: % L)-% m % N, that is, the first five cells are used to display priority, then the current file name is displayed, and the current number of rows is added. The last is the information in logger. debug (), logger.info (), logger. Warn (), or logger. Error. % N indicates that the carriage return is empty.

Run the program and output the following:

Debug (logtest. Java: 9)-Debug...

Info (logtest. Java: 10)-info...

Warn (logtest. Java: 11)-warn...

Error (logtest. Java: 12)-error...

After a long time, I finally output such a thing? Yes. Someone said, "Not as good as system. Out. println ". But let's see what you did.

1. Download log4j and copy the class library to the lib directory of the project.

2. Write a log4j. properties file (4 rows)

3. added"StaticLogger logger = logger. getlogger (logtest.Class. Getname (); "one sentence

4. Use logger. xxx to output information.

Finished. Some of the benefits you get are:

1. In the log4j. properties file, rewrite "log4j. rootcategory = debug, stdout" to "log4j. rootcategory = OFF, stdout", so that all log information will not be displayed;

2. In log4j. in the properties file, set "log4j. rootcategory = debug, stdout "to" log4j. rootcategory = info, stdout ". In this way, only the log information of info, warn, and error is displayed, while the debug information is not displayed;

3. In the log4j. properties file, rewrite "log4j. rootcategory = debug, stdout" to "log4j. rootcategory = debug, stdout, R", and add 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. layout. conversionpattern = % d {yyyy Mmm dd hh: mm: SS} %-5 p % C-% m % N

In this way, your log information is not only displayed on the screen, but also saved in a file named "log.txt". The maximum size of the file is 100kb. If the file size exceeds kb, the file will be backed up to "log.txt. 1", and the new "log.txt" will continue to record log information.

You can change log4j. properties without re-compiling to control whether the log information is displayed, the log output type, output mode, and output format. The four steps above have brought so many benefits. Do I need system. Out. println to display log information? No.

 

The directory structure of my logtest project is as follows:

Content of the build. xml file:

<Project

Name = "log4j test" default = "build" basedir = ".">

<Property name = "app. Home" value = "$ {basedir}"/>

<Property name = "app. SRC" value = "$ {app. Home}/src"/>

<Property name = "app. bin" value = "$ {app. Home}/bin"/>

<Property name = "app. lib" value = "$ {app. Home}/lib"/>

<Path id = "classpath">

<Fileset dir = "$ {app. Lib}">

<Include name = "**/*. Jar"/>

</Fileset>

<Path location = "$ {app. Home}"/>

<Path location = "$ {app. Bin}"/>

</Path>

<Target name = "init">

<Mkdir dir = "$ {app. Bin}"/>

</Target>

<Target name = "build" depends = "init" Description = "compile the source">

<Javac srcdir = "$ {app. SRC}" destdir = "$ {app. Bin}">

<Classpath refID = "classpath"/>

</Javac>

</Target>

<Target name = "run" Description = "run">

<Java classname = "logtest" dir = "$ {app. Bin}" fork = "true">

<Classpath refID = "classpath"/>

</Java>

</Target>

<Target name = "clean" Description = "Clean Up">

<Delete dir = "$ {app. Bin}"/>

</Target>

</Project>

A total of four files, a logtest class to test, a build. xml ant file, a log4j. properties configuration file, and a log4j-1.2.8.jar class library.

Log4j has many functions. doodoofish is not detailed here. The following references:

  • Don't use system. Out. println! Use log4j by vipan SINGLA

  • Build flexible logs with log4j by Vikram Goyal

  • Log4j by Ashley j.s Mills, University of birmheim

  • Add logging to your Java applications by Kevin Brown

  • How does the Java logging API stack up against log4j? By Kevin Brown

  • Opensymphony logging Primer

  • Log4j FAQ at jguru

     

  • Related Article

    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.