Detailed explanation of Log4 (transfer)

1.1 Relevant English detailed explanation
 log: log debug: debugging error: error warn: warning, reminder info: message
1.2 What is log4j? Why log4j?
 Log4j definition: log4j is a popular and excellent logging tool, which can output log information in various flexible ways;
 why use log4j: when a program error occurs, we hope that it can be saved in the form of a file This exception information
 can be used for logging in order to view the processing optimization program in the future. The most popular one is log4j,which is open sourceand easy to use;
1.3 How to use log4j to record the exception log sent by the program information?
    (1). Download the jar package of log4j. log4j-1.2.15.jar
    (2). Please paste the jar package into the project.
    (3). Import the jar package: project name, right click--properties--java build path --libraries--add jars...
         The second step can not be executed, in the third step, the project name, right-click --properties--java build path--libraries--add External jars...you can
    distinguish: add jars... ... is to add the jar package in the project, add External jars... is not limited to the package, and the jar package is not placed in the project;
    recommended to perform the second step to realize the integration of the project and the jar package to avoid unknown exceptions
    ( 4). Create a new log4j.properties: src--right-click--new--file--name log4j.properties
    (5). Configure log4j.properties:
   ### Set the priority, and output the output source stdout to the console, and save the file to the file ###
   log4j.rootLogger=debug, stdout, file

   ### Output log information to console ###
   log4j.appender.stdout=org.apache.log4j.ConsoleAppender
   log4j.appender.stdout.Target=System.out
   log4j.appender.stdout.layout=org.apache.log4j .PatternLayout
   log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %m%n


   ### Output log information to a file: accp.log ###
   log4j.appender.file=org.apache.log4j.FileAppender
   log4j.appender.file.File=accp.log
   log4j.appender.file.layout=org. apache.log4j.PatternLayout
   log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %m%n
    (6). Detailed configuration:
   log4j.appender.stdout=org. apache.log4j.ConsoleAppender: add to console 
   log4j.appender.stdout.Target=System.out: mode is output  
   log4j.appender.stdout.layout=org.apache.log4j.PatternLayout: information layout mode is custom
   log4j.appender .stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %m%
   nLayout:Layout:yyyy-year mm-month dd-day hh-hour mm-minute ss- Seconds %l: Specific exception code lines %m: Exception information %n: Information line feed
    (7) Priority:   The
        priority from high to low is ERROR, WARN, INFO, DEBUG
    (8). Summary:
     a: The configuration file cannot contain Chinese (not even in Chinese), because the configuration file saving standard is: "ISO-8859-1";
     b: log4j.rootLogger=debug, stdout, the debug in the file can be replaced with ERROR, WARN , INFO, DEBUG 
     c: Set the level according to the requirements, if it is set to debug, all exceptions lower than its level will be ignored
     d: log4j.rootLogger=debug, stdout, stdout in file, file can choose one of them

1.4 Example description:
 Requirement: Requirement: The calculation class (compute) requires the user to input two numbers to perform division and save the result to process with exception class and use log4j to record exception information
Code implementation:
package com.t97.compute;

import java.util.InputMismatchException;
import java.util.Scanner;
import org.apache.log4j.Logger;//导入log4j包

public class Compute {
 // declare array
 private int[] num = new int[2];

 // Encapsulate property
 public int[] getNum() {
  return num;
 }

 public void setNum (int [] num) {
  this.num = num;
 }

 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  Compute compute = new Compute();
  Logger log = Logger.getLogger(Compute.class);
  int result = 0;
  int[] num = compute.getNum();
  String answer = null;
  try {
   System.out.println("Please enter the first number: ");
   num[0] = input.nextInt();
   System.out.println(" Please enter the second number: ");
   num[1] = input.nextInt();
   result = num[0] / num[1];
   System.out.println("Save the result?(y/n)" );
   answer = input.next();
   if (answer.equalsIgnoreCase("y")) {
    num[2] = result;// try to save the result of the calculation to an array
   }
  } catch (InputMismatchException e) {
   System.out.println("User input data type is wrong!");
   log.debug("User input data type is wrong!");
  } catch (ArithmeticException ae) {
   System.out.println("An arithmetic error occurred!" + ae.getMessage());
   log.info("Arithmetic error occurred!" + ae.getMessage());
  } catch (ArrayIndexOutOfBoundsException aee) {
   System.out.println("Array out of bounds!");
   log.warn( aee);
  } catch (Exception exception) {
   System.out.println("An exception occurred in the program, causing inconvenience to you, please restart!");
   log.error(exception);
  } finally {
   System.out.println( num[0] + " divided by " + num[1] + " evaluates to: " + result);
  }

 }
}

1.4.1 Log analysis:
Pre-record 1: When the user input is not a number, InputMismatchException catches an exception, ends the program, and outputs a prompt: User input data type is wrong!
log.debug("User input data type is wrong!"), record the error message:

  2009-09-28 11:36:23 com.t97.compute.Compute.main(Compute.java:46) User input data type is wrong!
Pre-record 2: When the second number input by the user is the divisor of 0, ArithmeticException catches an exception, ends the program, and outputs a prompt: Arithmetic error occurred! / by zero
 log.info("An arithmetic error occurred!" + ae.getMessage()), log the error message:

  2009-09-28 11:35:00 com.t97.compute.Compute.main(Compute.java:49) java.lang.ArithmeticException: / by zero
Pre-record 3: Program prompt: "Do you want to save the result? (y/ n)", if after entering "Y", ArrayIndexOutOfBoundsException catches an exception, ends the program, and outputs a prompt: The array is out of bounds because the data length is 2, and the program tries to "num[2] = result;// Try to save the calculation result to In the array", out of bounds occurred!
          log.warn(aee), log error messages:

  2009-09-28 11:37:41 com.t97.compute.Compute.main(Compute.java:52) java.lang.ArrayIndexOutOfBoundsException: 2
Pre-record 4: Unknown error, Exception superclass caught the exception, ended the program, The output prompts "an exception occurred in the program, causing inconvenience to you, please restart!"
          log.error(exception), record the error message: (unknown...)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326392523&siteId=291194637