MATSIM
OutputDirectoryLogging.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * OutputDirectoryLogging.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007, 2008 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.controler;
22 
23 import java.io.IOException;
24 import java.nio.file.FileSystems;
25 
26 import org.apache.logging.log4j.Logger;
27 import org.apache.logging.log4j.Level;
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.core.Appender;
30 import org.apache.logging.log4j.core.LogEvent;
31 import org.apache.logging.log4j.core.LoggerContext;
32 import org.apache.logging.log4j.core.appender.FileAppender;
33 import org.apache.logging.log4j.core.config.Configuration;
34 import org.matsim.core.gbl.Gbl;
36 
50 public final class OutputDirectoryLogging {
51  private OutputDirectoryLogging(){} // do not instantiate
52 
53  public static final String LOGFILE = "logfile.log";
54 
55  public static final String WARNLOGFILE = "logfileWarningsErrors.log";
56 
57  private static Logger log = LogManager.getLogger(OutputDirectoryLogging.class);
58 
64 
65  public static void catchLogEntries() {
66  if ( collectLogMessagesAppender != null ) {
67  // create a new instance only if there is not one yet, to allow
68  // collecting log messages issued _before_ controller construction.
69  // Otherwise, all log messages before the last call to this method
70  // are lost.
71  // td, mar. 2013
72  return;
73  }
74 
75  collectLogMessagesAppender = new CollectLogMessagesAppender();
76  collectLogMessagesAppender.start();
77  {
78  final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
79  ctx.getConfiguration().getRootLogger().addAppender(collectLogMessagesAppender, Level.ALL, null);
80  ctx.updateLoggers();
81  }
82  }
83 
88  public final static void initLogging(OutputDirectoryHierarchy outputDirectoryHierarchy) {
89  if (collectLogMessagesAppender != null) {
90  final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
91  ctx.getConfiguration().getRootLogger().removeAppender(collectLogMessagesAppender.getName());
92  ctx.updateLoggers();
93  }
94  try {
95  String outputFilename = outputDirectoryHierarchy.getOutputFilename(LOGFILE);
96  String warnLogfileName = outputDirectoryHierarchy.getOutputFilename(WARNLOGFILE);
97  initLogging(outputFilename, warnLogfileName);
98  } catch (IOException e) {
99  log.error("Cannot create logfiles: " + e.getMessage());
100  e.printStackTrace();
101  }
102  }
103 
115  public static void initLoggingWithOutputDirectory(final String outputDirectory) throws IOException {
116  if (collectLogMessagesAppender != null) {
117  final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
118  ctx.getConfiguration().getRootLogger().removeAppender(collectLogMessagesAppender.getName());
119  ctx.updateLoggers();
120  }
121  String logfilename = outputDirectory + FileSystems.getDefault().getSeparator() + LOGFILE;
122  String warnlogfilename = outputDirectory + FileSystems.getDefault().getSeparator() + WARNLOGFILE;
123  initLogging(logfilename, warnlogfilename);
124  }
125 
126  private static void initLogging(String outputFilename, String warnLogfileName) throws IOException {
127  // code inspired from http://logging.apache.org/log4j/2.x/manual/customconfig.html#AddingToCurrent
128 
129  final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
130  final Configuration config = ctx.getConfiguration();
131  final boolean appendToExistingFile = false;
132 
133  FileAppender appender;
134  { // the "all" logfile
135  appender = FileAppender.newBuilder().setName(LOGFILE).setLayout(Controler.DEFAULTLOG4JLAYOUT).withFileName(outputFilename).withAppend(appendToExistingFile).build();
136  appender.start();
137  config.getRootLogger().addAppender(appender, Level.ALL, null);
138  }
139 
140  FileAppender warnErrorAppender;
141  { // the "warnings and errors" logfile
142  warnErrorAppender = FileAppender.newBuilder().setName(WARNLOGFILE).setLayout(Controler.DEFAULTLOG4JLAYOUT).withFileName(warnLogfileName).withAppend(appendToExistingFile).build();
143  warnErrorAppender.start();
144  config.getRootLogger().addAppender(warnErrorAppender, Level.WARN, null);
145  }
146 
147  ctx.updateLoggers();
148 
149  if (collectLogMessagesAppender != null) {
150  LogEvent e;
151  while ((e = collectLogMessagesAppender.getLogEvents().poll()) != null) {
152  appender.append(e);
153  if (e.getLevel().isMoreSpecificThan(Level.WARN)) {
154  warnErrorAppender.append(e);
155  }
156  }
157  collectLogMessagesAppender.stop();
158  collectLogMessagesAppender = null;
159  }
163  }
164 
170  public static void closeOutputDirLogging() {
171  //might also be sent to the warn logstream but then you end up with a warning even if everything is alright
172  String endLoggingInfo = "closing the logfile, i.e. messages sent to the logger after this message are not written to the logfile.";
173  log.info(endLoggingInfo);
174  LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
175  org.apache.logging.log4j.core.Logger root = LoggerContext.getContext(false).getRootLogger();
176  Appender app = root.getAppenders().get(LOGFILE);
177  if (app != null) {
178  root.removeAppender(app);
179  app.stop();
180  }
181  app = root.getAppenders().get(WARNLOGFILE);
182  if (app != null) {
183  root.removeAppender(app);
184  app.stop();
185  }
186  ctx.updateLoggers();
187  }
188 }
static final PatternLayout DEFAULTLOG4JLAYOUT
Definition: Controler.java:114
static final void printRunCommand()
Definition: Gbl.java:75
static CollectLogMessagesAppender collectLogMessagesAppender
static void initLogging(String outputFilename, String warnLogfileName)
static void initLoggingWithOutputDirectory(final String outputDirectory)
static final void printBuildInfo()
Definition: Gbl.java:108
static final void printSystemInfo()
Definition: Gbl.java:63
static final void initLogging(OutputDirectoryHierarchy outputDirectoryHierarchy)