Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2015 Cisco Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
*/
package com.cisco.oss.foundation.logging;
import com.cisco.oss.foundation.flowcontext.FlowContextFactory;
import com.cisco.oss.foundation.logging.structured.AbstractFoundationLoggingMarker;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.*;
import org.apache.log4j.helpers.Loader;
import org.apache.log4j.helpers.OptionConverter;
import org.apache.log4j.nt.NTEventLogAppender;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.RepositorySelector;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.slf4j.Marker;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;
import org.slf4j.spi.LocationAwareLogger;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.*;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
/**
* This Logger implementation is used for applying the Foundation Logging standards on
* top of log4j. The logger is initialized by the the FoundationLogHierarcy Object. It
* is not meant to be used in code by developers. In runtime they will use this
* Logger if they specify the following line in thier log4j.properties file:
* log4j.loggerFactory=com.cisco.oss.foundation.logging.FoundationLogFactory
*
* @author Yair Ogen
*/
class FoundationLogger extends Logger implements LocationAwareLogger { // NOPMD
/**
* the property key for reloading the log4j properties file.
*/
private static final String Foundation_FILE_RELOAD_DELAY = "FoundationfileReloadDelay";
/**
* default delay value for reloading the log4j properties file.
*/
private static final int FILE_RELOAD_DELAY = 10000;
static Properties log4jConfigProps = null; // NOPMD
private static final String DEFAULT_CONFIGURATION_FILE = "log4j.properties"; // NOPMD
private static final String DEFAULT_CONFIGURATION_KEY = "log4j.configuration"; // NOPMD
private static final String FQCN = FoundationLogger.class.getName();
private static final String PATTERN_KEY = "messagePattern";
public static Map> markerAppendersMap = new HashMap>();
/**
* Boolean indicating whether or not NTEventLogAppender is supported.
*/
private static boolean ntEventLogSupported = true;
FoundationLogger(final String name) {
super(name);
}
/**
* Initialize that Foundation Logging library.
*/
static void init() {// NOPMD
determineIfNTEventLogIsSupported();
URL resource = null;
final String configurationOptionStr = OptionConverter.getSystemProperty(DEFAULT_CONFIGURATION_KEY, null);
if (configurationOptionStr != null) {
try {
resource = new URL(configurationOptionStr);
} catch (MalformedURLException ex) {
// so, resource is not a URL:
// attempt to get the resource from the class path
resource = Loader.getResource(configurationOptionStr);
}
}
if (resource == null) {
resource = Loader.getResource(DEFAULT_CONFIGURATION_FILE); // NOPMD
}
if (resource == null) {
System.err.println("[FoundationLogger] Can not find resource: " + DEFAULT_CONFIGURATION_FILE); // NOPMD
throw new FoundationIOException("Can not find resource: " + DEFAULT_CONFIGURATION_FILE); // NOPMD
}
// update the log manager to use the Foundation repository.
final RepositorySelector foundationRepositorySelector = new FoundationRepositorySelector(FoundationLogFactory.foundationLogHierarchy);
LogManager.setRepositorySelector(foundationRepositorySelector, null);
// set logger to info so we always want to see these logs even if root
// is set to ERROR.
final Logger logger = getLogger(FoundationLogger.class);
final String logPropFile = resource.getPath();
log4jConfigProps = getLogProperties(resource);
// select and configure again so the loggers are created with the right
// level after the repository selector was updated.
OptionConverter.selectAndConfigure(resource, null, FoundationLogFactory.foundationLogHierarchy);
// start watching for property changes
setUpPropFileReloading(logger, logPropFile, log4jConfigProps);
// add syslog appender or windows event viewer appender
// setupOSSystemLog(logger, log4jConfigProps);
// parseMarkerPatterns(log4jConfigProps);
// parseMarkerPurePattern(log4jConfigProps);
// udpateMarkerStructuredLogOverrideMap(logger);
AbstractFoundationLoggingMarker.init();
updateSniffingLoggersLevel(logger);
setupJULSupport(resource);
}
private static void setupJULSupport(URL resource) {
boolean julSupportEnabled = Boolean.valueOf(log4jConfigProps.getProperty(FoundationLoggerConstants.Foundation_JUL_SUPPORT_ENABLED.toString(), "false"));
if (julSupportEnabled) {
String appenderRef = log4jConfigProps.getProperty(FoundationLoggerConstants.Foundation_JUL_APPENDER_REF.toString());
if (StringUtils.isBlank(appenderRef)) {
Enumeration allAppenders = Logger.getRootLogger().getAllAppenders();
while (allAppenders.hasMoreElements()) {
Appender appender = (Appender) allAppenders.nextElement();
if (appender instanceof FileAppender) {
appenderRef = appender.getName();
getLogger(FoundationLogger.class).info("*** Using '" + appenderRef + "' as the Java util logging appender ref ***");
System.err.println("*** Using '" + appenderRef + "' as the Java util logging appender ref ***");
break;
}
}
}
if (StringUtils.isBlank(appenderRef)) {
throw new IllegalArgumentException("Java util support was enabled but couldn't find a matching appender under the '" + FoundationLoggerConstants.Foundation_JUL_APPENDER_REF.toString() + "' key.");
}
Handler handler = null;
Appender appender = Logger.getRootLogger().getAppender(appenderRef);
if(appender == null){
Enumeration allAppenders = Logger.getRootLogger().getAllAppenders();
while (allAppenders.hasMoreElements()){
Appender tempAppender = (Appender)allAppenders.nextElement();
if(tempAppender instanceof AsyncAppender){
AsyncAppender asyncAppender = (AsyncAppender)tempAppender;
Enumeration asyncAppenderAllAppenders = asyncAppender.getAllAppenders();
while (asyncAppenderAllAppenders.hasMoreElements()){
Appender asyncTempAppender = (Appender)asyncAppenderAllAppenders.nextElement();
if(appenderRef.equals(asyncTempAppender.getName())){
appender = asyncTempAppender;
break;
}
}
if(appender != null){
break;
}
}
}
}
if(appender instanceof FileAppender){
try {
handler = new FileHandler(((FileAppender)appender).getFile());
} catch (IOException e) {
throw new IllegalArgumentException("IOException encountered when trying to setup jul logging: " + e, e);
}
}else if(appender instanceof ConsoleAppender){
handler = new ConsoleHandler();
}else{
getLogger(FoundationLogger.class).error("got a reference to an unsupported appender: " + appenderRef);
}
if (handler != null) {
// System.setProperty("java.util.logging.config.file",resource.getPath());
java.util.logging.LogManager.getLogManager().reset();
try {
java.util.logging.LogManager.getLogManager().readConfiguration(resource.openStream());
} catch (IOException e) {
throw new IllegalArgumentException("IOException encountered when trying to read log4j properties file: " + e, e);
}
handler.setLevel(java.util.logging.Level.FINEST);
handler.setFormatter(new FoundationLogFormatter());
java.util.logging.Logger rootLogger = java.util.logging.Logger.getLogger("");
rootLogger.addHandler(handler);
rootLogger.setLevel(java.util.logging.Level.SEVERE);
Properties julLoggerSubset = getPropertiesSubset("jul.logger");
if(!julLoggerSubset.isEmpty()){
Set