The branch, master has been updated via 0124e54b54f81e10f5980b8796c80a12d4f87069 (commit) from 50f843f7507c7a206046205d742fe7bd4e305581 (commit)
Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- http://git.qos.ch/gitweb/?p=slf4j.git;a=commit;h=0124e54b54f81e10f5980b8796c80a12d4f87069 http://github.com/ceki/slf4j/commit/0124e54b54f81e10f5980b8796c80a12d4f87069 commit 0124e54b54f81e10f5980b8796c80a12d4f87069 Author: Ceki Gulcu <c...@qos.ch> Date: Tue Sep 1 22:50:54 2009 +0200 - Localication code that actually does something useful diff --git a/slf4j-ext/src/main/java/org/slf4j/cal10n/LLogger.java b/slf4j-ext/src/main/java/org/slf4j/cal10n/LLogger.java deleted file mode 100644 index 08917d8..0000000 --- a/slf4j-ext/src/main/java/org/slf4j/cal10n/LLogger.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2004-2009 QOS.ch All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.slf4j.cal10n; - -import org.slf4j.Logger; -import org.slf4j.ext.LoggerWrapper; - -public class LLogger extends LoggerWrapper implements Logger { - - private static final String FQCN = LLogger.class.getName(); - - public LLogger(Logger logger) { - super(logger, FQCN); - } - - void debug(Enum<?> e, Object... args) { - - } - - -} diff --git a/slf4j-ext/src/main/java/org/slf4j/cal10n/LocLogger.java b/slf4j-ext/src/main/java/org/slf4j/cal10n/LocLogger.java new file mode 100644 index 0000000..4ce9d70 --- /dev/null +++ b/slf4j-ext/src/main/java/org/slf4j/cal10n/LocLogger.java @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2004-2009 QOS.ch All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.slf4j.cal10n; + +import java.util.Locale; + +import org.slf4j.Logger; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; +import org.slf4j.ext.LoggerWrapper; +import org.slf4j.spi.LocationAwareLogger; + +import ch.qos.cal10n.IMessageConveyor; +import ch.qos.cal10n.MessageConveyor; +import ch.qos.cal10n.MessageParameterObj; + +/** + * A logger specialized in localized logging. Localization is based in the <a + * href="http://cal10n.qos.ch">CAL10N project</p>. + * + * @author Ceki Gülcü + */ +public class LocLogger extends LoggerWrapper implements Logger { + + private static final String FQCN = LocLogger.class.getName(); + + /** + * Every localized message logged by a LocLogger will bear this marker. It + * allows marker-aware implementations to perform additional processing on + * localized messages. + */ + static Marker LOCALIZED = MarkerFactory.getMarker("LOCALIZED"); + + final Locale locale; + final IMessageConveyor imc; + + public LocLogger(Logger logger, Locale locale) { + super(logger, LoggerWrapper.class.getName()); + this.locale = locale; + imc = new MessageConveyor(locale); + } + + /** + * Log a localized message at the TRACE level. + * + * @param key + * the key used for localization + * @param args + * optional arguments + */ + public void trace(Enum<?> key, Object... args) { + if (!logger.isTraceEnabled()) { + return; + } + String translatedMsg = imc.getMessage(key, args); + MessageParameterObj mpo = new MessageParameterObj(key, args); + + if (instanceofLAL) { + ((LocationAwareLogger) logger).log(LOCALIZED, FQCN, + LocationAwareLogger.TRACE_INT, translatedMsg, null); + } else { + logger.trace(LOCALIZED, translatedMsg, mpo); + } + } + + /** + * Log a localized message at the DEBUG level. + * + * @param key + * the key used for localization + * @param args + * optional arguments + */ + public void debug(Enum<?> key, Object... args) { + if (!logger.isDebugEnabled()) { + return; + } + String translatedMsg = imc.getMessage(key, args); + MessageParameterObj mpo = new MessageParameterObj(key, args); + + if (instanceofLAL) { + ((LocationAwareLogger) logger).log(LOCALIZED, FQCN, + LocationAwareLogger.DEBUG_INT, translatedMsg, null); + } else { + logger.debug(LOCALIZED, translatedMsg, mpo); + } + } + + /** + * Log a localized message at the INFO level. + * + * @param key + * the key used for localization + * @param args + * optional arguments + */ + public void info(Enum<?> key, Object... args) { + if (!logger.isInfoEnabled()) { + return; + } + String translatedMsg = imc.getMessage(key, args); + MessageParameterObj mpo = new MessageParameterObj(key, args); + + if (instanceofLAL) { + ((LocationAwareLogger) logger).log(LOCALIZED, FQCN, + LocationAwareLogger.INFO_INT, translatedMsg, null); + } else { + logger.info(LOCALIZED, translatedMsg, mpo); + } + } + + /** + * Log a localized message at the WARN level. + * + * @param key + * the key used for localization + * @param args + * optional arguments + */ + public void warn(Enum<?> key, Object... args) { + if (!logger.isWarnEnabled()) { + return; + } + String translatedMsg = imc.getMessage(key, args); + MessageParameterObj mpo = new MessageParameterObj(key, args); + + if (instanceofLAL) { + ((LocationAwareLogger) logger).log(LOCALIZED, FQCN, + LocationAwareLogger.WARN_INT, translatedMsg, null); + } else { + logger.warn(LOCALIZED, translatedMsg, mpo); + } + } + + /** + * Log a localized message at the ERROR level. + * + * @param key + * the key used for localization + * @param args + * optional arguments + */ + public void error(Enum<?> key, Object... args) { + if (!logger.isErrorEnabled()) { + return; + } + String translatedMsg = imc.getMessage(key, args); + MessageParameterObj mpo = new MessageParameterObj(key, args); + + if (instanceofLAL) { + ((LocationAwareLogger) logger).log(LOCALIZED, FQCN, + LocationAwareLogger.ERROR_INT, translatedMsg, null); + } else { + logger.error(LOCALIZED, translatedMsg, mpo); + } + } + +} diff --git a/slf4j-ext/src/main/java/org/slf4j/cal10n/LLoggerFactory.java b/slf4j-ext/src/main/java/org/slf4j/cal10n/LocLoggerFactory.java similarity index 55% rename from slf4j-ext/src/main/java/org/slf4j/cal10n/LLoggerFactory.java rename to slf4j-ext/src/main/java/org/slf4j/cal10n/LocLoggerFactory.java index 5ba4f7b..d744104 100644 --- a/slf4j-ext/src/main/java/org/slf4j/cal10n/LLoggerFactory.java +++ b/slf4j-ext/src/main/java/org/slf4j/cal10n/LocLoggerFactory.java @@ -21,29 +21,53 @@ */ package org.slf4j.cal10n; +import java.util.Locale; + +import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class LLoggerFactory { +/** + * + * This class is essentially a wrapper around an {...@link LoggerFactory} producing + * {...@link LocLogger} instances. + * + * <p> + * Contrary to {...@link LoggerFactory#getLogger(String)} method of + * {...@link LoggerFactory}, each call to {...@link getLocLogger} produces a new + * instance of {...@link LocLogger}. This should not matter because an XLogger + * instance does have any state beyond that of the {...@link Logger} instance + * it wraps and its locale. + * + * @author Ceki Gulcu + * + */ +public class LocLoggerFactory { + + final Locale locale; + + public LocLoggerFactory(Locale locale) { + this.locale = locale; + } /** - * Get an LLogger instance by name. + * Get an LocLogger instance by name. * * @param name * @return */ - public static LLogger getLLogger(String name) { - return new LLogger(LoggerFactory.getLogger(name)); + public LocLogger getLocLogger(String name) { + return new LocLogger(LoggerFactory.getLogger(name), locale); } /** - * Get a new LLogger instance by class. The returned XLogger - * will be named after the class. + * Get a new LocLogger instance by class. The returned LocLogger will be named + * after the class. * * @param clazz * @return */ @SuppressWarnings("unchecked") - public static LLogger getLLogger(Class clazz) { - return getLLogger(clazz.getName()); + public LocLogger getLocLogger(Class clazz) { + return getLocLogger(clazz.getName()); } } diff --git a/slf4j-ext/src/main/java/org/slf4j/ext/LoggerWrapper.java b/slf4j-ext/src/main/java/org/slf4j/ext/LoggerWrapper.java index 6204d08..8d42297 100644 --- a/slf4j-ext/src/main/java/org/slf4j/ext/LoggerWrapper.java +++ b/slf4j-ext/src/main/java/org/slf4j/ext/LoggerWrapper.java @@ -22,10 +22,10 @@ public class LoggerWrapper implements Logger { // fqcn depend on the caller, but its value would not be different // between successive invocations of a factory class - final Logger logger; + protected final Logger logger; final String fqcn; // is this logger instance a LocationAwareLogger - final boolean instanceofLAL; + protected final boolean instanceofLAL; public LoggerWrapper(Logger logger, String fqcn) { this.logger = logger; diff --git a/slf4j-ext/src/test/java/org/slf4j/cal10n_dummy/LocLoggerTest.java b/slf4j-ext/src/test/java/org/slf4j/cal10n_dummy/LocLoggerTest.java new file mode 100644 index 0000000..48e4c51 --- /dev/null +++ b/slf4j-ext/src/test/java/org/slf4j/cal10n_dummy/LocLoggerTest.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2004-2009 QOS.ch + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package org.slf4j.cal10n_dummy; + +import java.util.Locale; + +import junit.framework.TestCase; + +import org.apache.log4j.spi.LoggingEvent; +import org.slf4j.cal10n.LocLogger; +import org.slf4j.cal10n.LocLoggerFactory; +import org.slf4j.dummyExt.ListAppender; + +public class LocLoggerTest extends TestCase { + + ListAppender listAppender; + org.apache.log4j.Logger log4jRoot; + + LocLoggerFactory llFactory_uk = new LocLoggerFactory(Locale.UK); + + final static String EXPECTED_FILE_NAME = "LocLoggerTest.java"; + + public LocLoggerTest(String name) { + super(name); + } + + public void setUp() throws Exception { + super.setUp(); + + // start from a clean slate for each test + + listAppender = new ListAppender(); + listAppender.extractLocationInfo = true; + log4jRoot = org.apache.log4j.Logger.getRootLogger(); + log4jRoot.addAppender(listAppender); + log4jRoot.setLevel(org.apache.log4j.Level.TRACE); + } + + void verify(LoggingEvent le, String expectedMsg) { + assertEquals(expectedMsg, le.getMessage()); + assertEquals(EXPECTED_FILE_NAME, le.getLocationInformation().getFileName()); + } + + public void tearDown() throws Exception { + super.tearDown(); + } + + public void testSmoke() { + LocLogger locLogger = llFactory_uk.getLocLogger(this.getClass()); + locLogger.info(Months.JAN); + verify((LoggingEvent) listAppender.list.get(0), "January"); + + } +} diff --git a/slf4j-ext/src/test/java/org/slf4j/cal10n_dummy/Months.java b/slf4j-ext/src/test/java/org/slf4j/cal10n_dummy/Months.java new file mode 100644 index 0000000..679e7c4 --- /dev/null +++ b/slf4j-ext/src/test/java/org/slf4j/cal10n_dummy/Months.java @@ -0,0 +1,10 @@ +package org.slf4j.cal10n_dummy; + +import ch.qos.cal10n.ResourceBundleName; + +...@resourcebundlename("months") +public enum Months { + + JAN, FEB, MAR, APR, MAY, JUN; + +} diff --git a/slf4j-ext/src/test/resources/months_en.properties b/slf4j-ext/src/test/resources/months_en.properties new file mode 100644 index 0000000..e3e20a1 --- /dev/null +++ b/slf4j-ext/src/test/resources/months_en.properties @@ -0,0 +1,6 @@ +JAN=January +FEB=February +MAR=March +APR=April +MAY=May +JUN=June ----------------------------------------------------------------------- Summary of changes: .../src/main/java/org/slf4j/cal10n/LLogger.java | 40 ----- .../src/main/java/org/slf4j/cal10n/LocLogger.java | 177 ++++++++++++++++++++ .../{LLoggerFactory.java => LocLoggerFactory.java} | 40 ++++- .../src/main/java/org/slf4j/ext/LoggerWrapper.java | 4 +- .../LocLoggerTest.java} | 62 +++++--- .../test/java/org/slf4j/cal10n_dummy/Months.java | 10 + slf4j-ext/src/test/resources/months_en.properties | 6 + 7 files changed, 267 insertions(+), 72 deletions(-) delete mode 100644 slf4j-ext/src/main/java/org/slf4j/cal10n/LLogger.java create mode 100644 slf4j-ext/src/main/java/org/slf4j/cal10n/LocLogger.java rename slf4j-ext/src/main/java/org/slf4j/cal10n/{LLoggerFactory.java => LocLoggerFactory.java} (55%) copy slf4j-ext/src/test/java/org/slf4j/{NDCTest.java => cal10n_dummy/LocLoggerTest.java} (50%) create mode 100644 slf4j-ext/src/test/java/org/slf4j/cal10n_dummy/Months.java create mode 100644 slf4j-ext/src/test/resources/months_en.properties hooks/post-receive -- SLF4J: Simple Logging Facade for Java _______________________________________________ dev mailing list dev@slf4j.org http://www.slf4j.org/mailman/listinfo/dev