Skip to content

Commit

Permalink
Core: Fix location information for loggers
Browse files Browse the repository at this point in the history
This change corrects the location information gathered by the loggers so that when printing class name, method name, and line numbers in the log pattern, the information from the class calling the logger is used rather than a location within the logger itself.

A reset method has also been added to the LogConfigurator class which allows the logging configuration to be reset. This is needed because if the LoggingConfigurationTests and Log4jESLoggerTests are run in the same JVM the second one to run needs to be able to override the log configuration set by the first

Closes #5130, #8052
  • Loading branch information
snuffkin authored and colings86 committed Oct 30, 2014
1 parent ce77814 commit 5930740
Show file tree
Hide file tree
Showing 10 changed files with 498 additions and 37 deletions.
106 changes: 106 additions & 0 deletions src/main/java/org/elasticsearch/common/logging/jdk/ESLogRecord.java
@@ -0,0 +1,106 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.common.logging.jdk;

import org.elasticsearch.common.logging.support.AbstractESLogger;

import java.util.logging.Level;
import java.util.logging.LogRecord;

/**
* A {@link LogRecord} which is used in conjunction with {@link JdkESLogger}
* with the ability to provide the class name, method name and line number
* information of the code calling the logger
*/
public class ESLogRecord extends LogRecord {

private static final long serialVersionUID = 1107741560233585726L;
private static final String FQCN = AbstractESLogger.class.getName();
private String sourceClassName;
private String sourceMethodName;
private transient boolean needToInferCaller;

public ESLogRecord(Level level, String msg) {
super(level, msg);
needToInferCaller = true;
}

public String getSourceClassName() {
if (needToInferCaller) {
inferCaller();
}
return sourceClassName;
}

public void setSourceClassName(String sourceClassName) {
this.sourceClassName = sourceClassName;
needToInferCaller = false;
}

public String getSourceMethodName() {
if (needToInferCaller) {
inferCaller();
}
return sourceMethodName;
}

public void setSourceMethodName(String sourceMethodName) {
this.sourceMethodName = sourceMethodName;
needToInferCaller = false;
}

/**
* Determines the source information for the caller of the logger (class
* name, method name, and line number)
*/
private void inferCaller() {
needToInferCaller = false;
Throwable throwable = new Throwable();

boolean lookingForLogger = true;
for (final StackTraceElement frame : throwable.getStackTrace()) {
String cname = frame.getClassName();
boolean isLoggerImpl = isLoggerImplFrame(cname);
if (lookingForLogger) {
// Skip all frames until we have found the first logger frame.
if (isLoggerImpl) {
lookingForLogger = false;
}
} else {
if (!isLoggerImpl) {
// skip reflection call
if (!cname.startsWith("java.lang.reflect.") && !cname.startsWith("sun.reflect.")) {
// We've found the relevant frame.
setSourceClassName(cname);
setSourceMethodName(frame.getMethodName());
return;
}
}
}
}
// We haven't found a suitable frame, so just punt. This is
// OK as we are only committed to making a "best effort" here.
}

private boolean isLoggerImplFrame(String cname) {
// the log record could be created for a platform logger
return cname.equals(FQCN);
}
}
45 changes: 31 additions & 14 deletions src/main/java/org/elasticsearch/common/logging/jdk/JdkESLogger.java
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.common.logging.support.AbstractESLogger;

import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
Expand All @@ -31,12 +32,9 @@ public class JdkESLogger extends AbstractESLogger {

private final Logger logger;

private final String name;

public JdkESLogger(String prefix, String name, Logger logger) {
public JdkESLogger(String prefix, Logger logger) {
super(prefix);
this.logger = logger;
this.name = name;
}

@Override
Expand Down Expand Up @@ -96,51 +94,70 @@ public boolean isErrorEnabled() {

@Override
protected void internalTrace(String msg) {
logger.logp(Level.FINEST, name, null, msg);
LogRecord record = new ESLogRecord(Level.FINEST, msg);
logger.log(record);
}

@Override
protected void internalTrace(String msg, Throwable cause) {
logger.logp(Level.FINEST, name, null, msg, cause);
LogRecord record = new ESLogRecord(Level.FINEST, msg);
record.setThrown(cause);
logger.log(record);
}

@Override
protected void internalDebug(String msg) {
logger.logp(Level.FINE, name, null, msg);
LogRecord record = new ESLogRecord(Level.FINE, msg);
logger.log(record);
}

@Override
protected void internalDebug(String msg, Throwable cause) {
logger.logp(Level.FINE, name, null, msg, cause);
LogRecord record = new ESLogRecord(Level.FINE, msg);
record.setThrown(cause);
logger.log(record);
}

@Override
protected void internalInfo(String msg) {
logger.logp(Level.INFO, name, null, msg);
LogRecord record = new ESLogRecord(Level.INFO, msg);
logger.log(record);
}

@Override
protected void internalInfo(String msg, Throwable cause) {
logger.logp(Level.INFO, name, null, msg, cause);
LogRecord record = new ESLogRecord(Level.INFO, msg);
record.setThrown(cause);
logger.log(record);
}

@Override
protected void internalWarn(String msg) {
logger.logp(Level.WARNING, name, null, msg);
LogRecord record = new ESLogRecord(Level.WARNING, msg);
logger.log(record);
}

@Override
protected void internalWarn(String msg, Throwable cause) {
logger.logp(Level.WARNING, name, null, msg, cause);
LogRecord record = new ESLogRecord(Level.WARNING, msg);
record.setThrown(cause);
logger.log(record);
}

@Override
protected void internalError(String msg) {
logger.logp(Level.SEVERE, name, null, msg);
LogRecord record = new ESLogRecord(Level.SEVERE, msg);
logger.log(record);
}

@Override
protected void internalError(String msg, Throwable cause) {
logger.logp(Level.SEVERE, name, null, msg, cause);
LogRecord record = new ESLogRecord(Level.SEVERE, msg);
record.setThrown(cause);
logger.log(record);
}

protected Logger logger() {
return logger;
}
}
Expand Up @@ -22,8 +22,6 @@
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;

import java.util.logging.LogManager;

/**
*
*/
Expand All @@ -37,6 +35,6 @@ protected ESLogger rootLogger() {
@Override
protected ESLogger newInstance(String prefix, String name) {
final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(name);
return new JdkESLogger(prefix, name, logger);
return new JdkESLogger(prefix, logger);
}
}
Expand Up @@ -29,6 +29,7 @@
public class Log4jESLogger extends AbstractESLogger {

private final org.apache.log4j.Logger logger;
private final String FQCN = AbstractESLogger.class.getName();

public Log4jESLogger(String prefix, Logger logger) {
super(prefix);
Expand Down Expand Up @@ -95,51 +96,51 @@ public boolean isErrorEnabled() {

@Override
protected void internalTrace(String msg) {
logger.trace(msg);
logger.log(FQCN, Level.TRACE, msg, null);
}

@Override
protected void internalTrace(String msg, Throwable cause) {
logger.trace(msg, cause);
logger.log(FQCN, Level.TRACE, msg, cause);
}

@Override
protected void internalDebug(String msg) {
logger.debug(msg);
logger.log(FQCN, Level.DEBUG, msg, null);
}

@Override
protected void internalDebug(String msg, Throwable cause) {
logger.debug(msg, cause);
logger.log(FQCN, Level.DEBUG, msg, cause);
}

@Override
protected void internalInfo(String msg) {
logger.info(msg);
logger.log(FQCN, Level.INFO, msg, null);
}

@Override
protected void internalInfo(String msg, Throwable cause) {
logger.info(msg, cause);
logger.log(FQCN, Level.INFO, msg, cause);
}

@Override
protected void internalWarn(String msg) {
logger.warn(msg);
logger.log(FQCN, Level.WARN, msg, null);
}

@Override
protected void internalWarn(String msg, Throwable cause) {
logger.warn(msg, cause);
logger.log(FQCN, Level.WARN, msg, cause);
}

@Override
protected void internalError(String msg) {
logger.error(msg);
logger.log(FQCN, Level.ERROR, msg, null);
}

@Override
protected void internalError(String msg, Throwable cause) {
logger.error(msg, cause);
logger.log(FQCN, Level.ERROR, msg, cause);
}
}
Expand Up @@ -101,6 +101,14 @@ public static void configure(Settings settings) {
PropertyConfigurator.configure(props);
}

/**
* sets the loaded flag to false so that logging configuration can be
* overridden. Should only be used in tests.
*/
public static void reset() {
loaded = false;
}

public static void resolveConfig(Environment env, final ImmutableSettings.Builder settingsBuilder) {

try {
Expand Down

0 comments on commit 5930740

Please sign in to comment.