Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for Log4J 2 like for Log4J
- Loading branch information
Showing
10 changed files
with
233 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
javamelody-core/src/main/java/net/bull/javamelody/Log4J2Appender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright 2008-2016 by Emeric Vernat | ||
* | ||
* This file is part of Java Melody. | ||
* | ||
* 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 net.bull.javamelody; | ||
|
||
import java.io.Serializable; | ||
|
||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.logging.log4j.core.Appender; | ||
import org.apache.logging.log4j.core.Filter; | ||
import org.apache.logging.log4j.core.Layout; | ||
import org.apache.logging.log4j.core.LogEvent; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.apache.logging.log4j.core.appender.AbstractAppender; | ||
import org.apache.logging.log4j.core.config.AbstractConfiguration; | ||
import org.apache.logging.log4j.core.config.LoggerConfig; | ||
import org.apache.logging.log4j.core.filter.ThresholdFilter; | ||
import org.apache.logging.log4j.core.layout.PatternLayout; | ||
|
||
/** | ||
* Appender pour les logs de log4j2, configuré automatiquement par {@link MonitoringFilter}. | ||
* @author Emeric Vernat | ||
*/ | ||
public class Log4J2Appender extends AbstractAppender { | ||
private static final long serialVersionUID = 1L; | ||
private static final String MESSAGE_PATTERN = "%-5p [%c] %m%n"; | ||
private static final Level THRESHOLD = Level.WARN; | ||
|
||
private static final String APPENDER_NAME = Log4J2Appender.class.getName(); | ||
private static final PatternLayout LAYOUT = PatternLayout.createLayout(MESSAGE_PATTERN, null, | ||
null, null, null, true, true, null, null); | ||
private static final ThresholdFilter FILTER = ThresholdFilter.createFilter(THRESHOLD, null, | ||
null); | ||
|
||
private static final Log4J2Appender SINGLETON = new Log4J2Appender(); | ||
|
||
/** | ||
* Constructeur. | ||
*/ | ||
public Log4J2Appender() { | ||
this(APPENDER_NAME, FILTER, LAYOUT); | ||
} | ||
|
||
/** | ||
* Constructeur. | ||
* @param name String | ||
* @param filter Filter | ||
* @param layout Layout | ||
*/ | ||
public Log4J2Appender(final String name, final Filter filter, | ||
final Layout<? extends Serializable> layout) { | ||
super(name, filter, layout, true); | ||
} | ||
|
||
static Log4J2Appender getSingleton() { | ||
return SINGLETON; | ||
} | ||
|
||
void register() { | ||
if (LogManager.getContext(false) instanceof LoggerContext) { | ||
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); | ||
if (ctx.getConfiguration() instanceof AbstractConfiguration) { | ||
final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration(); | ||
final Appender appender = getSingleton(); | ||
appender.start(); | ||
config.addAppender(appender); | ||
final Logger rootLogger = LogManager.getRootLogger(); | ||
final LoggerConfig loggerConfig = config.getLoggerConfig(rootLogger.getName()); | ||
loggerConfig.addAppender(appender, null, null); | ||
ctx.updateLoggers(); | ||
} | ||
} | ||
} | ||
|
||
void deregister() { | ||
if (LogManager.getContext(false) instanceof LoggerContext) { | ||
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); | ||
if (ctx.getConfiguration() instanceof AbstractConfiguration) { | ||
final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration(); | ||
final Appender appender = getSingleton(); | ||
appender.stop(); | ||
config.removeAppender(appender.getName()); | ||
final Logger rootLogger = LogManager.getRootLogger(); | ||
final LoggerConfig loggerConfig = config.getLoggerConfig(rootLogger.getName()); | ||
loggerConfig.removeAppender(appender.getName()); | ||
ctx.updateLoggers(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void append(final LogEvent event) { | ||
final Throwable throwable = event.getThrown(); | ||
final String message = getLayout().toSerializable(event).toString(); | ||
LoggingHandler.addErrorLogToCounter(message, throwable); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
javamelody-core/src/main/java/net/bull/javamelody/Log4J2Logger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2008-2016 by Emeric Vernat | ||
* | ||
* This file is part of Java Melody. | ||
* | ||
* 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 net.bull.javamelody; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* JavaMelodyLogger pour Log4J 2. | ||
* @author Emeric Vernat | ||
*/ | ||
class Log4J2Logger implements JavaMelodyLogger { | ||
private static final org.apache.logging.log4j.Logger LOGGER = org.apache.logging.log4j.LogManager | ||
.getLogger(INTERNAL_LOGGER_NAME); | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public void info(String msg) { | ||
LOGGER.info(msg); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public void info(String msg, Throwable throwable) { | ||
LOGGER.info(msg, throwable); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public void warn(String msg, Throwable throwable) { | ||
LOGGER.warn(msg, throwable); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public void debug(String msg) { | ||
LOGGER.debug(msg); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public void debug(String msg, Throwable throwable) { | ||
LOGGER.debug(msg, throwable); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public void logHttpRequest(HttpServletRequest httpRequest, String requestName, long duration, | ||
boolean systemError, int responseSize, String loggerName) { | ||
final org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager | ||
.getLogger(loggerName); | ||
if (logger.isInfoEnabled()) { | ||
logger.info(LOG.buildLogMessage(httpRequest, duration, systemError, responseSize)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
<%@page import="org.slf4j.LoggerFactory"%> | ||
<%@page import="org.apache.log4j.Logger"%> | ||
<%@page import="org.apache.logging.log4j.LogManager"%> | ||
<%@ page session="false" %> | ||
|
||
<% | ||
Logger.getRootLogger().warn("test Log4J"); | ||
Logger.getLogger("test").warn("test Log4J"); | ||
LogManager.getLogger("test").error("test Log4J 2"); | ||
LoggerFactory.getLogger("test").warn("test Logback"); | ||
java.util.logging.Logger.getGlobal().warning("test java.util.logging"); | ||
%> | ||
|
||
Warning logged in Log4J, and in Logback and in java.util.logging. | ||
Warning logged in Log4J and in Log4J 2 and in Logback and in java.util.logging. | ||
<br/> | ||
<a href="../index.jsp">back</a> |