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 /** The default format to use when formatting dates */ 037 protected static final String DEFAULT_DATE_TIME_FORMAT = "yyyy/MM/dd HH:mm:ss:SSS zzz"; 038 039 /** All system properties used by <code>SimpleLog</code> start with this */ 040 protected static final String SYSTEM_PREFIX = "org.apache.logging.log4j.simplelog."; 041 042 private final PropertiesUtil props; 043 044 /** Include the instance name in the log message? */ 045 private final boolean showLogName; 046 047 /** 048 * Include the short name (last component) of the logger in the log message. Defaults to true - otherwise we'll be 049 * lost in a flood of messages without knowing who sends them. 050 */ 051 private final boolean showShortName; 052 /** Include the current time in the log message */ 053 private final boolean showDateTime; 054 /** Include the ThreadContextMap in the log message */ 055 private final boolean showContextMap; 056 /** The date and time format to use in the log message */ 057 private final String dateTimeFormat; 058 059 private final Level defaultLevel; 060 061 private final PrintStream stream; 062 063 private final LoggerRegistry<ExtendedLogger> loggerRegistry = new LoggerRegistry<>(); 064 065 public SimpleLoggerContext() { 066 props = new PropertiesUtil("log4j2.simplelog.properties"); 067 068 showContextMap = props.getBooleanProperty(SYSTEM_PREFIX + "showContextMap", false); 069 showLogName = props.getBooleanProperty(SYSTEM_PREFIX + "showlogname", false); 070 showShortName = props.getBooleanProperty(SYSTEM_PREFIX + "showShortLogname", true); 071 showDateTime = props.getBooleanProperty(SYSTEM_PREFIX + "showdatetime", false); 072 final String lvl = props.getStringProperty(SYSTEM_PREFIX + "level"); 073 defaultLevel = Level.toLevel(lvl, Level.ERROR); 074 075 dateTimeFormat = showDateTime ? props.getStringProperty(SimpleLoggerContext.SYSTEM_PREFIX + "dateTimeFormat", 076 DEFAULT_DATE_TIME_FORMAT) : null; 077 078 final String fileName = props.getStringProperty(SYSTEM_PREFIX + "logFile", "system.err"); 079 PrintStream ps; 080 if ("system.err".equalsIgnoreCase(fileName)) { 081 ps = System.err; 082 } else if ("system.out".equalsIgnoreCase(fileName)) { 083 ps = System.out; 084 } else { 085 try { 086 final FileOutputStream os = new FileOutputStream(fileName); 087 ps = new PrintStream(os); 088 } catch (final FileNotFoundException fnfe) { 089 ps = System.err; 090 } 091 } 092 this.stream = ps; 093 } 094 095 @Override 096 public ExtendedLogger getLogger(final String name) { 097 return getLogger(name, null); 098 } 099 100 @Override 101 public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) { 102 // Note: This is the only method where we add entries to the 'loggerRegistry' ivar. 103 final ExtendedLogger extendedLogger = loggerRegistry.getLogger(name, messageFactory); 104 if (extendedLogger != null) { 105 AbstractLogger.checkMessageFactory(extendedLogger, messageFactory); 106 return extendedLogger; 107 } 108 final SimpleLogger simpleLogger = new SimpleLogger(name, defaultLevel, showLogName, showShortName, showDateTime, 109 showContextMap, dateTimeFormat, messageFactory, props, stream); 110 loggerRegistry.putIfAbsent(name, messageFactory, simpleLogger); 111 return loggerRegistry.getLogger(name, messageFactory); 112 } 113 114 @Override 115 public boolean hasLogger(final String name) { 116 return false; 117 } 118 119 @Override 120 public boolean hasLogger(String name, MessageFactory messageFactory) { 121 return false; 122 } 123 124 @Override 125 public boolean hasLogger(String name, Class<? extends MessageFactory> messageFactoryClass) { 126 return false; 127 } 128 129 @Override 130 public Object getExternalContext() { 131 return null; 132 } 133 134}