001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.simple; 018 019import java.io.FileNotFoundException; 020import java.io.FileOutputStream; 021import java.io.PrintStream; 022 023import org.apache.logging.log4j.Level; 024import org.apache.logging.log4j.message.MessageFactory; 025import org.apache.logging.log4j.spi.AbstractLogger; 026import org.apache.logging.log4j.spi.ExtendedLogger; 027import org.apache.logging.log4j.spi.LoggerContext; 028import org.apache.logging.log4j.spi.LoggerRegistry; 029import org.apache.logging.log4j.util.PropertiesUtil; 030 031/** 032 * 033 */ 034public class SimpleLoggerContext implements LoggerContext { 035 036 private static final String SYSTEM_OUT = "system.out"; 037 038 private static final String SYSTEM_ERR = "system.err"; 039 040 /** The default format to use when formatting dates */ 041 protected static final String DEFAULT_DATE_TIME_FORMAT = "yyyy/MM/dd HH:mm:ss:SSS zzz"; 042 043 /** All system properties used by <code>SimpleLog</code> start with this */ 044 protected static final String SYSTEM_PREFIX = "org.apache.logging.log4j.simplelog."; 045 046 private final PropertiesUtil props; 047 048 /** Include the instance name in the log message? */ 049 private final boolean showLogName; 050 051 /** 052 * Include the short name (last component) of the logger in the log message. Defaults to true - otherwise we'll be 053 * lost in a flood of messages without knowing who sends them. 054 */ 055 private final boolean showShortName; 056 /** Include the current time in the log message */ 057 private final boolean showDateTime; 058 /** Include the ThreadContextMap in the log message */ 059 private final boolean showContextMap; 060 /** The date and time format to use in the log message */ 061 private final String dateTimeFormat; 062 063 private final Level defaultLevel; 064 065 private final PrintStream stream; 066 067 private final LoggerRegistry<ExtendedLogger> loggerRegistry = new LoggerRegistry<>(); 068 069 public SimpleLoggerContext() { 070 props = new PropertiesUtil("log4j2.simplelog.properties"); 071 072 showContextMap = props.getBooleanProperty(SYSTEM_PREFIX + "showContextMap", false); 073 showLogName = props.getBooleanProperty(SYSTEM_PREFIX + "showlogname", false); 074 showShortName = props.getBooleanProperty(SYSTEM_PREFIX + "showShortLogname", true); 075 showDateTime = props.getBooleanProperty(SYSTEM_PREFIX + "showdatetime", false); 076 final String lvl = props.getStringProperty(SYSTEM_PREFIX + "level"); 077 defaultLevel = Level.toLevel(lvl, Level.ERROR); 078 079 dateTimeFormat = showDateTime ? props.getStringProperty(SimpleLoggerContext.SYSTEM_PREFIX + "dateTimeFormat", 080 DEFAULT_DATE_TIME_FORMAT) : null; 081 082 final String fileName = props.getStringProperty(SYSTEM_PREFIX + "logFile", SYSTEM_ERR); 083 PrintStream ps; 084 if (SYSTEM_ERR.equalsIgnoreCase(fileName)) { 085 ps = System.err; 086 } else if (SYSTEM_OUT.equalsIgnoreCase(fileName)) { 087 ps = System.out; 088 } else { 089 try { 090 final FileOutputStream os = new FileOutputStream(fileName); 091 ps = new PrintStream(os); 092 } catch (final FileNotFoundException fnfe) { 093 ps = System.err; 094 } 095 } 096 this.stream = ps; 097 } 098 099 @Override 100 public ExtendedLogger getLogger(final String name) { 101 return getLogger(name, null); 102 } 103 104 @Override 105 public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) { 106 // Note: This is the only method where we add entries to the 'loggerRegistry' ivar. 107 final ExtendedLogger extendedLogger = loggerRegistry.getLogger(name, messageFactory); 108 if (extendedLogger != null) { 109 AbstractLogger.checkMessageFactory(extendedLogger, messageFactory); 110 return extendedLogger; 111 } 112 final SimpleLogger simpleLogger = new SimpleLogger(name, defaultLevel, showLogName, showShortName, showDateTime, 113 showContextMap, dateTimeFormat, messageFactory, props, stream); 114 loggerRegistry.putIfAbsent(name, messageFactory, simpleLogger); 115 return loggerRegistry.getLogger(name, messageFactory); 116 } 117 118 @Override 119 public boolean hasLogger(final String name) { 120 return false; 121 } 122 123 @Override 124 public boolean hasLogger(final String name, final MessageFactory messageFactory) { 125 return false; 126 } 127 128 @Override 129 public boolean hasLogger(final String name, final Class<? extends MessageFactory> messageFactoryClass) { 130 return false; 131 } 132 133 @Override 134 public Object getExternalContext() { 135 return null; 136 } 137 138}