vcjmhg edited a comment on issue #4545:
URL: https://github.com/apache/skywalking/issues/4545#issuecomment-729419758


   # Summary
   
   The plug-in can store the logs generated by the program during the call, 
such as the content of the error log, into the span log. And the plug-in 
provides matching files. Through the configuration file, you can control the 
log source (log4j2, logbak, log4j), package name, level, and content that need 
to be stored in the span log, and you can customize the log format saved to the 
span log. .
   
   # Configuration file
   
   `logfilter.yaml`
   
   ```yaml
     - name: "log4j2"
       packages:
         - package1
         - package2
       level: "trace"
       pattern: "%date %level [%thread] %logger{10} [%file:%line] %msg%n"
       expression: "Regular expression"
     - name: "logbak"
       packages:
         - package3
       level: "error"
       patterns: "%msg%n"
       expression: "Regular expression"
   ```
   
   # Field description
   
   **name attribute**: Specify the log system that needs to perform log 
conversion. If you use a configuration file for configuration, the name must be 
written.
   
   There are four values for name:
   
   * log4j
   * log4j2
   * logbak
   * `*`:Match all supported log
   
   **package attribute**: Specify the package name of the log that needs log 
conversion. When using the configuration file for configuration, it can be 
defaulted. If the default is stupid with `*`, it will match all packages.
   
   `package value`:
   
   * the name of package, eg: `org.apache.skywalking`
   * `*`:match all 
   
   **level attribute**: The level of the log for conversion. **By default it is 
`error` level**
   
   * trace
   * debug
   * info
   * error
   
   **pattern attribute:** used to control the format of the log written to the 
span log
   
   The control method of the specific format, refer to logbak, for details, 
please refer to http://logback.qos.ch/manual/layouts.html
   
   **expression attribute**: filter logs that need to be converted into lspan 
log. This attribute accepts a regular expression, the content of the log body 
will be matched, if it is matched, then input, otherwise no conversion will be 
performed. **By default, it matches all, that is, no filtering is performed. **
   
   # Realization idea
   
   1. Analyze the configuration file and fill it into a  instance of 
`loggerConfig`, which contains a List to store Logger, which is a record of 
configuration information of the logging system.
   
   2. After the error method is intercepted, the log level will be determined 
according to the configuration file. **If the level requirements are met**, the 
regular expression filtering will begin. If the filtering requirements are met, 
the instance of `LogEvent` will be created. After filling, execute the formate 
method to format the log information.
   
      **Formatting operation method (modeled on PatternLayout to realize log 
analysis):**
   
      1. The `LoggerFactory.getLogger()` method is enhanced to obtain the 
logger object. And then get the  object of `LoggerContext` .
   
         ```java
         Logger logger = LoggerFactory.getLogger(CaseController.class);
                 ch.qos.logback.classic.Logger myLogger = 
(ch.qos.logback.classic.Logger) logger;
                 LoggerContext context = myLogger.getLoggerContext();
         ```
   
      2. Rewrite a suitable Appender or use `PatternLayoutEncorder` directly to 
format the required information
   
         ```java
          Logger rootLogger = 
(Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
             LoggerContext loggerContext = rootLogger.getLoggerContext();
             // we are not interested in auto-configuration
             loggerContext.reset();
         
             PatternLayoutEncoder encoder = new PatternLayoutEncoder();
             encoder.setContext(loggerContext);
             encoder.setPattern("%-5level [%thread]: %message%n");
             encoder.start();
         
             ConsoleAppender<ILoggingEvent> appender = new 
ConsoleAppender<ILoggingEvent>();
             appender.setContext(loggerContext);
             appender.setEncoder(encoder); 
             appender.start();
         
             rootLogger.addAppender(appender);
         
             rootLogger.debug("Message 1");
             rootLogger.warn("Message 2");
         ```
   
   3. After the operation is completed, initialize the instance of 
`ContextCarrier` to obtain the span and then place the corresponding 
information in the `spanlog`
   
   4. Stop `sapn`
   
   # Some problems
   
   1. `LogbakLoggerFactoryInstrumentation` is not working well and I can't get 
the the instance of `org.slf4j.LoggerFactory`.Now I am fixing it.
   2. The function of formatting the log has not been fully completed yet.
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to