How Tomcat works seven Logger

Source: Internet
Author: User
You can just breathe a sigh of relief. This component is relatively simple. It is much simpler than the previous sections.

Logger interface

Logger in Tomcat must implement the org. Apache. Catalina. logger interface.

package org.apache.catalina;import java.beans.PropertyChangeListener;public interface Logger {        public static final int FATAL = Integer.MIN_VALUE;    public static final int ERROR = 1;    public static final int WARNING = 2;    public static final int INFORMATION = 3;    public static final int DEBUG = 4;    public Container getContainer();    public void setContainer(Container container);    public String getInfo();    public int getVerbosity();    public void setVerbosity(int verbosity);    public void addPropertyChangeListener(PropertyChangeListener listener);    public void log(String message);    public void log(Exception exception, String msg);    public void log(String message, Throwable throwable);    public void log(String message, int verbosity);    public void log(String message, Throwable throwable, int verbosity);    public void removePropertyChangeListener(PropertyChangeListener listener);}


Logger defines five types of log records. When we call the log (string message, int verbosity) method to record logs, only the passed verbosity is smaller than or equal to the default value of the system. Do you know the value of integer. min_value? About 2.1 billion negative. What does setverbosity and getverbosity do?

Tomcat Logger
The loggerbase class (abstract class) implements other methods except the log (string message) method in the logger interface;
In the class, the default log record level is
Protected int verbosity = error;
However, we can reset the record level through setverbosity (INT verbosity;
Let's take a look at the two log methods defined at the log acceptance level.
public void log(String message, int verbosity) {    if (this.verbosity >= verbosity)        log(message);}public void log(String message, Throwable throwable, int verbosity) {    if (this.verbosity >= verbosity)        log(message, throwable);}

The systemoutlogger class knows the name, and the information is finally output to the console.
The systeerrlogger class can be identified by its name. It is an error output and the information is still in the console, but it is red.
The filelogger class knows its name. It is in the file output by information.
But specifically, it is still a bit of a head-on. Let's take a look.
First, filelogger implements the lifecycle interface, so it can be started by the parent component like other components.
In this section, the start and stop methods of filelogger only change the Boolean value of started, without doing anything else. On the other hand, the start and stop methods of filelogger are not called!
Now let's look at how to write information to the file.
 
public void log(String msg) {        // Construct the timestamp we will use, if requested        Timestamp ts = new Timestamp(System.currentTimeMillis());        String tsString = ts.toString().substring(0, 19);        String tsDate = tsString.substring(0, 10);        System.out.println("tsString   "+tsString); //tsString   2014-10-20 15:25:27        System.out.println("ts   "+ts);            //ts   2014-10-20 15:25:27.406        System.out.println("tsDate   "+tsDate);        //tsDate   2014-10-20        // If the date has changed, switch log files        if (!date.equals(tsDate)) {            synchronized (this) {                if (!date.equals(tsDate)) {                    close();                    date = tsDate;                    open();                }            }        }        // Log this message, timestamped if necessary        if (writer != null) {            if (timestamp) {                writer.println(tsString + " " + msg);            } else {                writer.println(msg);            }        }    }


File is a new file every day by default. The file format is date + specific information (if the timestamp is true );
So what are the attributes of the close (), open () method and writer?
 
Private void open () {// create the directory if necessary file dir = new file (directory); If (! Dir. isabsolute () dir = new file (system. getproperty ("Catalina. base "), directory); // for Catalina. what is base? Let's talk about dir later. mkdirs (); // open the current log file try {string pathname = dir. getabsolutepath () + file. separator + prefix + date + suffix; writer = new printwriter (New filewriter (pathname, true), true);} catch (ioexception e) {writer = NULL ;}}

Check whether the specified directory exists. If no new one exists, set the file name based on the prefix, time, and suffix, and fill the writer object with the file name.
    private void close() {        if (writer == null)            return;        writer.flush();        writer.close();        writer = null;        date = "";    }

Date in filelogger indicates the current time. Of course, it is null after it is disabled.

Applications
Bootstrap class
...    System.setProperty("catalina.base", System.getProperty("user.dir"));    FileLogger logger = new FileLogger();    logger.setPrefix("FileLog_");    logger.setSuffix(".txt");    logger.setTimestamp(true);    logger.setDirectory("webroot");    context.setLogger(logger);....
Simplecontext class
public synchronized void start() throws LifecycleException {    log("starting Context");    ........    lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);    log("Context started");  }private void log(String message) {    Logger logger = this.getLogger();    if (logger!=null)      logger.log(message);  }



You can view the filelog_2014-10-20.txt file in the target webrootdirectory after running;
The following content is added to each running file:
14:34:21 httpconnector opening server socket on all Host IP addresses
14:34:22 httpconnector [8080] Starting background thread
14:34:22 httpprocessor [8080] [0] Starting background thread
14:34:22 httpprocessor [8080] [1] Starting background thread
14:34:22 httpprocessor [8080] [2] Starting background thread
14:34:22 httpprocessor [8080] [3] Starting background thread
14:34:22 httpprocessor [8080] [4] Starting background thread
2014-10-20 14:34:22 starting Context
2014-10-20 14:34:22 context started

How Tomcat works seven Logger

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.