Skip to content

Commit

Permalink
Include context metadata for Log4j2 and documentation update.
Browse files Browse the repository at this point in the history
RELNOTES=Include context metadata for Log4j2.
PiperOrigin-RevId: 379286898
  • Loading branch information
hagbard authored and Flogger Team committed Jun 14, 2021
1 parent 6fcdb0e commit 8846b42
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
* <li>{@code flogger.backend_factory=
* com.google.common.flogger.backend.log4j2.Log4j2BackendFactory#getInstance}
* </ul>
*
* <p>Note: Any changes in this code should, as far as possible, be reflected in the equivalently
* named log4j implementation. If the behaviour of this class starts to deviate from that of the
* log4j backend in any significant way, this difference should be called out clearly in the
* documentation.
*/
public final class Log4j2BackendFactory extends BackendFactory {
private static final Log4j2BackendFactory INSTANCE = new Log4j2BackendFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.common.flogger.backend.MessageUtils;
import com.google.common.flogger.backend.Metadata;
import com.google.common.flogger.backend.MetadataProcessor;
import com.google.common.flogger.backend.Platform;
import com.google.common.flogger.backend.SimpleMessageFormatter;
import java.util.Collections;
import java.util.Map;
Expand All @@ -37,14 +38,20 @@
import org.apache.logging.log4j.core.util.Throwables;
import org.apache.logging.log4j.message.SimpleMessage;

/** Helper to format LogData */
/** Helper to format LogData.
*
* <p>Note: Any changes in this code should, as far as possible, be reflected in the equivalently
* named log4j implementation. If the behaviour of this class starts to deviate from that of the
* log4j backend in any significant way, this difference should be called out clearly in the
* documentation.
*/
final class Log4j2LogEventUtil {

private Log4j2LogEventUtil() {}

static LogEvent toLog4jLogEvent(String loggerName, LogData logData) {
MetadataProcessor metadata =
MetadataProcessor.forScopeAndLogSite(Metadata.empty(), logData.getMetadata());
MetadataProcessor.forScopeAndLogSite(Platform.getInjectedMetadata(), logData.getMetadata());
String message = SimpleMessageFormatter.getDefaultFormatter().format(logData, metadata);
Throwable thrown = metadata.getSingleValue(LogContext.Key.LOG_CAUSE);
return toLog4jLogEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
import com.google.common.flogger.backend.LoggerBackend;
import org.apache.logging.log4j.core.Logger;

/** A logging backend that uses log4j2 to output log statements. */
/**
* A logging backend that uses log4j2 to output log statements.
*
* <p>Note: Any changes in this code should, as far as possible, be reflected in the equivalently
* named log4j implementation. If the behaviour of this class starts to deviate from that of the
* log4j backend in any significant way, this difference should be called out clearly in the
* documentation.
*/
final class Log4j2LoggerBackend extends LoggerBackend {
private final Logger logger;

Expand Down

0 comments on commit 8846b42

Please sign in to comment.