Repository: logging-log4j2 Updated Branches: refs/heads/master 05b236239 -> 4df0d3943
[LOG4J2-767] New module for Liquibase integration. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4df0d394 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4df0d394 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4df0d394 Branch: refs/heads/master Commit: 4df0d3943da873de043d68cb9ea114fb4f22c784 Parents: 05b2362 Author: Gary Gregory <[email protected]> Authored: Sat Jun 13 13:37:08 2015 -0700 Committer: Gary Gregory <[email protected]> Committed: Sat Jun 13 13:37:08 2015 -0700 ---------------------------------------------------------------------- log4j-liquibase/.gitignore | 3 + log4j-liquibase/pom.xml | 240 +++++++++++++++++++ .../ext/logging/log4j2/Log4j2Logger.java | 93 +++++++ log4j-liquibase/src/site/xdoc/index.xml | 48 ++++ .../ext/logging/log4j2/LoggingTest.java | 91 +++++++ .../src/test/resources/log4j2-test.xml | 14 ++ pom.xml | 6 + src/changes/changes.xml | 3 + 8 files changed, 498 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4df0d394/log4j-liquibase/.gitignore ---------------------------------------------------------------------- diff --git a/log4j-liquibase/.gitignore b/log4j-liquibase/.gitignore new file mode 100644 index 0000000..fbdf7eb --- /dev/null +++ b/log4j-liquibase/.gitignore @@ -0,0 +1,3 @@ +/.classpath +/.project +/.settings/ http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4df0d394/log4j-liquibase/pom.xml ---------------------------------------------------------------------- diff --git a/log4j-liquibase/pom.xml b/log4j-liquibase/pom.xml new file mode 100644 index 0000000..1fa4ac3 --- /dev/null +++ b/log4j-liquibase/pom.xml @@ -0,0 +1,240 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j</artifactId> + <version>2.4-SNAPSHOT</version> + <relativePath>../</relativePath> + </parent> + <artifactId>log4j-liquibase</artifactId> + <packaging>jar</packaging> + <name>Apache Log4j Liquibase Binding</name> + <description>The Apache Log4j Liquibase binding to Log4j 2 Core</description> + <properties> + <log4jParentDir>${basedir}/..</log4jParentDir> + <docLabel>Liquibase Documentation</docLabel> + <projectDir>/liquibase</projectDir> + </properties> + <dependencies> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + </dependency> + <dependency> + <groupId>org.liquibase</groupId> + <artifactId>liquibase-core</artifactId> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <excludes> + <exclude>**/*Test.java</exclude> + </excludes> + </configuration> + <executions> + <execution> + <id>default-tests</id> + <phase>test</phase> + <goals> + <goal>test</goal> + </goals> + <configuration> + <includes> + <include>**/*Test.java</include> + </includes> + <excludes> + <exclude>**/OptionalTest.java</exclude> + </excludes> + </configuration> + </execution> + <execution> + <id>test-optional</id> + <phase>test</phase> + <goals> + <goal>test</goal> + </goals> + <configuration> + <excludes> + <exclude>**/LoggerTest.java</exclude> + <exclude>**/MarkerTest.java</exclude> + <exclude>**/SerializeTest.java</exclude> + </excludes> + <includes> + <include>**/OptionalTest.java</include> + </includes> + </configuration> + </execution> + </executions> + </plugin> + <!-- Include the standard NOTICE and LICENSE --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-remote-resources-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>process</goal> + </goals> + <configuration> + <skip>false</skip> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <configuration> + <instructions> + <Export-Package> + liquibase.ext.logging.log4j2 + </Export-Package> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-changes-plugin</artifactId> + <version>${changes.plugin.version}</version> + <reportSets> + <reportSet> + <reports> + <report>changes-report</report> + </reports> + </reportSet> + </reportSets> + <configuration> + <issueLinkTemplate>%URL%/show_bug.cgi?id=%ISSUE%</issueLinkTemplate> + <useJql>true</useJql> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-checkstyle-plugin</artifactId> + <version>${checkstyle.plugin.version}</version> + <configuration> + <!--<propertiesLocation>${vfs.parent.dir}/checkstyle.properties</propertiesLocation> --> + <configLocation>${log4jParentDir}/checkstyle.xml</configLocation> + <suppressionsLocation>${log4jParentDir}/checkstyle-suppressions.xml</suppressionsLocation> + <enableRulesSummary>false</enableRulesSummary> + <propertyExpansion>basedir=${basedir}</propertyExpansion> + <propertyExpansion>licensedir=${log4jParentDir}/checkstyle-header.txt</propertyExpansion> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>${javadoc.plugin.version}</version> + <configuration> + <bottom><![CDATA[<p align="center">Copyright © {inceptionYear}-{currentYear} {organizationName}. All Rights Reserved.<br /> + Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, + and the Apache Log4j logo are trademarks of The Apache Software Foundation.</p>]]></bottom> + <!-- module link generation is completely broken in the javadoc plugin for a multi-module non-aggregating + project --> + <detectOfflineLinks>false</detectOfflineLinks> + <linksource>true</linksource> + </configuration> + <reportSets> + <reportSet> + <id>non-aggregate</id> + <reports> + <report>javadoc</report> + </reports> + </reportSet> + </reportSets> + </plugin> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>findbugs-maven-plugin</artifactId> + <version>${findbugs.plugin.version}</version> + <configuration> + <fork>true</fork> + <jvmArgs>-Duser.language=en</jvmArgs> + <threshold>Normal</threshold> + <effort>Default</effort> + <excludeFilterFile>${log4jParentDir}/findbugs-exclude-filter.xml</excludeFilterFile> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jxr-plugin</artifactId> + <version>${jxr.plugin.version}</version> + <reportSets> + <reportSet> + <id>non-aggregate</id> + <reports> + <report>jxr</report> + </reports> + </reportSet> + <reportSet> + <id>aggregate</id> + <reports> + <report>aggregate</report> + </reports> + </reportSet> + </reportSets> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-pmd-plugin</artifactId> + <version>${pmd.plugin.version}</version> + <configuration> + <targetJdk>${maven.compile.target}</targetJdk> + </configuration> + </plugin> + </plugins> + </reporting> +</project> + http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4df0d394/log4j-liquibase/src/main/java/liquibase/ext/logging/log4j2/Log4j2Logger.java ---------------------------------------------------------------------- diff --git a/log4j-liquibase/src/main/java/liquibase/ext/logging/log4j2/Log4j2Logger.java b/log4j-liquibase/src/main/java/liquibase/ext/logging/log4j2/Log4j2Logger.java new file mode 100644 index 0000000..96dd02c --- /dev/null +++ b/log4j-liquibase/src/main/java/liquibase/ext/logging/log4j2/Log4j2Logger.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 liquibase.ext.logging.log4j2; + +import liquibase.logging.core.AbstractLogger; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.spi.ExtendedLogger; + +/** + * Logs Liquidbase messages to Log4j 2.x. + * <p> + * This class must be in the {@code liquibase} package in order for the Liquibase plugin discovery mechanism to work. + * </p> + */ +public class Log4j2Logger extends AbstractLogger { + + private static final String FQCN = Log4j2Logger.class.getName(); + + private ExtendedLogger logger; + + @Override + public void debug(final String message) { + logger.logIfEnabled(FQCN, Level.DEBUG, null, buildMessage(message)); + } + + @Override + public void debug(final String message, final Throwable e) { + logger.logIfEnabled(FQCN, Level.DEBUG, null, buildMessage(message), e); + } + + @Override + public int getPriority() { + return 5; + } + + @Override + public void info(final String message) { + logger.logIfEnabled(FQCN, Level.INFO, null, buildMessage(message)); + } + + @Override + public void info(final String message, final Throwable e) { + logger.logIfEnabled(FQCN, Level.INFO, null, buildMessage(message), e); + } + + @Override + public void setLogLevel(final String logLevel, final String logFile) { + setLogLevel(logLevel); + // ignore logFile + } + + @Override + public void setName(final String name) { + logger = LogManager.getContext(false).getLogger(name); + } + + @Override + public void severe(final String message) { + logger.logIfEnabled(FQCN, Level.ERROR, null, buildMessage(message)); + } + + @Override + public void severe(final String message, final Throwable e) { + logger.logIfEnabled(FQCN, Level.ERROR, null, buildMessage(message), e); + } + + @Override + public void warning(final String message) { + logger.logIfEnabled(FQCN, Level.WARN, null, buildMessage(message)); + } + + @Override + public void warning(final String message, final Throwable e) { + logger.logIfEnabled(FQCN, Level.WARN, null, buildMessage(message), e); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4df0d394/log4j-liquibase/src/site/xdoc/index.xml ---------------------------------------------------------------------- diff --git a/log4j-liquibase/src/site/xdoc/index.xml b/log4j-liquibase/src/site/xdoc/index.xml new file mode 100644 index 0000000..f054f46 --- /dev/null +++ b/log4j-liquibase/src/site/xdoc/index.xml @@ -0,0 +1,48 @@ +<?xml version="1.0"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF 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. +--> + +<document> + <properties> + <title>Log4j 2 Liquibase Binding</title> + <author email="[email protected]">Mikael St�ldal</author> + </properties> + + <body> + <section name="Log4j 2 Liquibase Binding"> + + <p> + The Log4j 2 Liquibase Binding enables <a href="http://www.liquibase.org/">Liquibase</a> to log via Log4j 2. + </p> + + </section> + + <section name="Requirements"> + <p> + The Log4j 2 Liquibase Binding requires at least Java 6 and has a dependency + on the Log4j 2 API as well as the Liquibase core. + </p> + </section> + + <section name="Usage"> + <p> + Including this module will automatically make Liquibase log via Log4j 2, due the the plugin auto discovery mechanism of Liquibase. + </p> + </section> + + </body> +</document> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4df0d394/log4j-liquibase/src/test/java/liquibase/ext/logging/log4j2/LoggingTest.java ---------------------------------------------------------------------- diff --git a/log4j-liquibase/src/test/java/liquibase/ext/logging/log4j2/LoggingTest.java b/log4j-liquibase/src/test/java/liquibase/ext/logging/log4j2/LoggingTest.java new file mode 100644 index 0000000..b2917ed --- /dev/null +++ b/log4j-liquibase/src/test/java/liquibase/ext/logging/log4j2/LoggingTest.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 liquibase.ext.logging.log4j2; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import liquibase.logging.Logger; + +import org.apache.logging.log4j.core.util.Constants; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.Test; + +public class LoggingTest { + + private static final String NAME = "LoggerName"; + + static Logger logger; + + @BeforeClass + public static void setupClass() { + logger = new Log4j2Logger(); + logger.setName(NAME); + logger.setLogLevel("debug", null); + } + + @Test + public void debug() { + logger.debug("Debug message"); + verify(NAME + " " + getClass().getName() + " DEBUG Debug message" + Constants.LINE_SEPARATOR); + } + + @Test + public void info() { + logger.info("Info message"); + verify(NAME + " " + getClass().getName() + " INFO Info message" + Constants.LINE_SEPARATOR); + } + + @Test + public void warning() { + logger.warning("Warning message"); + verify(NAME + " " + getClass().getName() + " WARN Warning message" + Constants.LINE_SEPARATOR); + } + + @Test + public void severe() { + logger.severe("Severe message"); + verify(NAME + " " + getClass().getName() + " ERROR Severe message" + Constants.LINE_SEPARATOR); + } + + @Test + public void severeStacktrace() { + logger.severe("Severe message with stacktrace", new RuntimeException("thrown error")); + verify(NAME + " " + getClass().getName() + " ERROR Severe message with stacktrace" + Constants.LINE_SEPARATOR + + "java.lang.RuntimeException: thrown error"); + } + + private void verify(final String expected) { + final ListAppender listApp = ListAppender.getListAppender("List"); + assertNotNull("Missing Appender", listApp); + final List<String> events = listApp.getMessages(); + assertTrue("Incorrect number of messages. Expected 1 Actual " + events.size(), events.size() == 1); + final String actual = events.get(0); + assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual); + listApp.clear(); + } + + @After + public void cleanup() { + ListAppender.getListAppender("List").clear(); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4df0d394/log4j-liquibase/src/test/resources/log4j2-test.xml ---------------------------------------------------------------------- diff --git a/log4j-liquibase/src/test/resources/log4j2-test.xml b/log4j-liquibase/src/test/resources/log4j2-test.xml new file mode 100644 index 0000000..bc8adf1 --- /dev/null +++ b/log4j-liquibase/src/test/resources/log4j2-test.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="UTF-8"?> +<configuration status="error"> + <Appenders> + <List name="List"> + <PatternLayout pattern="%c %C %p %m%n%ex{1}"/> + </List> + </Appenders> + + <Loggers> + <Root level="DEBUG"> + <AppenderRef ref="List"/> + </Root> + </Loggers> +</configuration> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4df0d394/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 5216d6a..598e480 100644 --- a/pom.xml +++ b/pom.xml @@ -665,6 +665,11 @@ <version>0.0.6</version> </dependency> <dependency> + <groupId>org.liquibase</groupId> + <artifactId>liquibase-core</artifactId> + <version>3.3.5</version> + </dependency> + <dependency> <groupId>net.javacrumbs.json-unit</groupId> <artifactId>json-unit</artifactId> <version>1.1.6</version> @@ -1128,6 +1133,7 @@ <module>log4j-perf</module> <module>log4j-iostreams</module> <module>log4j-jul</module> + <module>log4j-liquibase</module> </modules> <profiles> <profile> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4df0d394/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 012b55a..4ab4ecf 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -24,6 +24,9 @@ </properties> <body> <release version="2.4" date="2015-MM-DD" description="GA Release 2.4"> + <action issue="LOG4J2-767" dev="ggregory" type="add" due-to="Mikael StÃ¥ldal"> + New module for Liquibase integration. + </action> <action issue="LOG4J2-1023" dev="ggregory" type="add" due-to="Mikael StÃ¥ldal"> New RewritePolicy for changing level of a log event. </action>
