LCOV - code coverage report
Current view: top level - pgm/util - ErrorLogFile.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 37 40 92.5 %
Date: 2022-11-19 15:00:39 Functions: 6 7 85.7 %

          Line data    Source code
       1             : // Copyright (C) 2009 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.pgm.util;
      16             : 
      17             : import com.google.gerrit.common.FileUtil;
      18             : import com.google.gerrit.extensions.events.LifecycleListener;
      19             : import com.google.gerrit.server.config.SitePaths;
      20             : import com.google.gerrit.server.util.SystemLog;
      21             : import com.google.gerrit.util.logging.LogTimestampFormatter;
      22             : import java.io.IOException;
      23             : import java.nio.file.Path;
      24             : import org.apache.log4j.ConsoleAppender;
      25             : import org.apache.log4j.Level;
      26             : import org.apache.log4j.LogManager;
      27             : import org.apache.log4j.Logger;
      28             : import org.apache.log4j.PatternLayout;
      29             : import org.eclipse.jgit.lib.Config;
      30             : 
      31           0 : public class ErrorLogFile {
      32             :   static final String LOG_NAME = "error_log";
      33             :   static final String JSON_SUFFIX = ".json";
      34             : 
      35             :   public static void errorOnlyConsole() {
      36          15 :     LogManager.resetConfiguration();
      37             : 
      38          15 :     PatternLayout layout = new PatternLayout();
      39          15 :     layout.setConversionPattern("%-5p %c %x: %m%n");
      40             : 
      41          15 :     ConsoleAppender dst = new ConsoleAppender();
      42          15 :     dst.setLayout(layout);
      43          15 :     dst.setTarget("System.err");
      44          15 :     dst.setThreshold(Level.ERROR);
      45          15 :     dst.activateOptions();
      46             : 
      47          15 :     Logger root = LogManager.getRootLogger();
      48          15 :     root.removeAllAppenders();
      49          15 :     root.addAppender(dst);
      50          15 :   }
      51             : 
      52             :   public static LifecycleListener start(Path sitePath, Config config, boolean consoleLog)
      53             :       throws IOException {
      54         138 :     Path logdir =
      55         138 :         FileUtil.mkdirsOrDie(new SitePaths(sitePath).logs_dir, "Cannot create log directory");
      56         138 :     if (SystemLog.shouldConfigure()) {
      57           8 :       initLogSystem(logdir, config, consoleLog);
      58             :     }
      59             : 
      60         138 :     return new LifecycleListener() {
      61             :       @Override
      62         138 :       public void start() {}
      63             : 
      64             :       @Override
      65             :       public void stop() {
      66         138 :         LogManager.shutdown();
      67         138 :       }
      68             :     };
      69             :   }
      70             : 
      71             :   private static void initLogSystem(Path logdir, Config config, boolean consoleLog) {
      72           8 :     Logger root = LogManager.getRootLogger();
      73           8 :     root.removeAllAppenders();
      74             : 
      75           8 :     PatternLayout errorLogLayout =
      76             :         new PatternLayout(
      77             :             "[%d{" + LogTimestampFormatter.TIMESTAMP_FORMAT + "}] [%t] %-5p %c %x: %m%n");
      78             : 
      79           8 :     if (consoleLog) {
      80           8 :       ConsoleAppender dst = new ConsoleAppender();
      81           8 :       dst.setLayout(errorLogLayout);
      82           8 :       dst.setTarget("System.err");
      83           8 :       dst.setThreshold(Level.INFO);
      84           8 :       dst.activateOptions();
      85             : 
      86           8 :       root.addAppender(dst);
      87             :     }
      88             : 
      89           8 :     boolean json = config.getBoolean("log", "jsonLogging", false);
      90           8 :     boolean text = config.getBoolean("log", "textLogging", true) || !(json || consoleLog);
      91           8 :     boolean rotate = config.getBoolean("log", "rotate", true);
      92             : 
      93           8 :     if (text) {
      94           8 :       root.addAppender(SystemLog.createAppender(logdir, LOG_NAME, errorLogLayout, rotate));
      95             :     }
      96             : 
      97           8 :     if (json) {
      98           0 :       root.addAppender(
      99           0 :           SystemLog.createAppender(
     100             :               logdir, LOG_NAME + JSON_SUFFIX, new ErrorLogJsonLayout(), rotate));
     101             :     }
     102           8 :   }
     103             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750