Author: andy Date: Sat Oct 19 16:12:49 2013 New Revision: 1533779 URL: http://svn.apache.org/r1533779 Log: More convenience operations (enabkle directly on SLF4J logger); reformat.
Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/logging/Log.java Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/logging/Log.java URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/logging/Log.java?rev=1533779&r1=1533778&r2=1533779&view=diff ============================================================================== --- jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/logging/Log.java (original) +++ jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/logging/Log.java Sat Oct 19 16:12:49 2013 @@ -16,7 +16,7 @@ * limitations under the License. */ -package org.apache.jena.atlas.logging; +package org.apache.jena.atlas.logging ; import java.io.ByteArrayInputStream ; import java.io.File ; @@ -39,382 +39,359 @@ import org.slf4j.LoggerFactory ; * until it is know that a log message is actually * required by level setting. */ -public class Log -{ +public class Log { private Log() {} - - static public void info(String caller, String msg) - { + + static public void info(String caller, String msg) { log(caller).info(msg) ; } - - static public void info(Object caller, String msg) - { + + static public void info(Object caller, String msg) { log(caller.getClass()).info(msg) ; } - - static public void info(Class<?> cls, String msg) - { + + static public void info(Class<? > cls, String msg) { log(cls).info(msg) ; } - - static public void info(Object caller, String msg, Throwable th) - { + + static public void info(Object caller, String msg, Throwable th) { log(caller.getClass()).info(msg, th) ; } - - static public void info(Class<?> cls, String msg, Throwable th) - { + + static public void info(Class<? > cls, String msg, Throwable th) { log(cls).info(msg, th) ; } - - static public void debug(String caller, String msg) - { + + static public void debug(String caller, String msg) { log(caller).info(msg) ; } - - static public void debug(Object caller, String msg) - { + + static public void debug(Object caller, String msg) { log(caller.getClass()).info(msg) ; } - - static public void debug(Class<?> cls, String msg) - { + + static public void debug(Class<? > cls, String msg) { log(cls).info(msg) ; } - - static public void debug(Object caller, String msg, Throwable th) - { + + static public void debug(Object caller, String msg, Throwable th) { log(caller.getClass()).info(msg, th) ; } - - static public void debug(Class<?> cls, String msg, Throwable th) - { + + static public void debug(Class<? > cls, String msg, Throwable th) { log(cls).info(msg, th) ; } - - static public void warn(String caller, String msg) - { + + static public void warn(String caller, String msg) { log(caller).warn(msg) ; } - - static public void warn(Object caller, String msg) - { + + static public void warn(Object caller, String msg) { warn(caller.getClass(), msg) ; } - static public void warn(Class<?> cls, String msg) - { + static public void warn(Class<? > cls, String msg) { log(cls).warn(msg) ; } - static public void warn(Object caller, String msg, Throwable th) - { + static public void warn(Object caller, String msg, Throwable th) { warn(caller.getClass(), msg, th) ; } - static public void warn(Class<?> cls, String msg, Throwable th) - { + static public void warn(Class<? > cls, String msg, Throwable th) { log(cls).warn(msg, th) ; } - static public void fatal(Object caller, String msg) - { + static public void fatal(Object caller, String msg) { fatal(caller.getClass(), msg) ; } - static public void fatal(Class<?> cls, String msg) - { + static public void fatal(Class<? > cls, String msg) { log(cls).error(msg) ; } - static public void fatal(Object caller, String msg, Throwable th) - { + static public void fatal(Object caller, String msg, Throwable th) { fatal(caller.getClass(), msg, th) ; } - static public void fatal(Class<?> cls, String msg, Throwable th) - { + static public void fatal(Class<? > cls, String msg, Throwable th) { log(cls).error(msg, th) ; } - static public void fatal(String caller, String msg) - { + static public void fatal(String caller, String msg) { log(caller).error(msg) ; } - - static public Logger log(Class<?> cls) - { + + static public Logger log(Class<? > cls) { return LoggerFactory.getLogger(cls) ; } - - static public Logger log(String loggerName) - { + + static public Logger log(String loggerName) { return LoggerFactory.getLogger(loggerName) ; } - - /** Turn on a logger (all levels). - * Works for Log4j and Java logging as the logging provider to Apache common logging or slf4j. + + /** + * Turn on a logger (all levels). Works for Log4j and Java logging as the + * logging provider to Apache common logging or slf4j. */ - static public void enable(String logger) - { + + static public void enable(Logger logger) { + enable(logger.getName()) ; + } + + static public void enable(String logger) { enable(logger, "all") ; } - + + static public void enable(Logger logger, String level) { + enable(logger.getName(), level) ; + } + /** Turn on a logger (specific level levels) */ - static public void enable(Class<?> logger, String level) { + static public void enable(Class<? > logger, String level) { enable(logger.getClass().getName(), level) ; } - - static public void enable(String logger, String level) - { + + static public void enable(String logger, String level) { org.apache.log4j.Level level1 = org.apache.log4j.Level.ALL ; java.util.logging.Level level2 = java.util.logging.Level.ALL ; - if ( level.equalsIgnoreCase("info")) - { + if ( level.equalsIgnoreCase("info") ) { level1 = org.apache.log4j.Level.INFO ; level2 = java.util.logging.Level.INFO ; } - if ( level.equalsIgnoreCase("debug")) - { + if ( level.equalsIgnoreCase("debug") ) { level1 = org.apache.log4j.Level.DEBUG ; level2 = java.util.logging.Level.FINE ; } - - if ( level.equalsIgnoreCase("warn")) - { + + if ( level.equalsIgnoreCase("warn") ) { level1 = org.apache.log4j.Level.WARN ; level2 = java.util.logging.Level.WARNING ; } - if ( level.equalsIgnoreCase("error")) - { + if ( level.equalsIgnoreCase("error") ) { level1 = org.apache.log4j.Level.ERROR ; level2 = java.util.logging.Level.SEVERE ; } logLevel(logger, level1, level2) ; } - - static public void logLevel(String logger, org.apache.log4j.Level level1, java.util.logging.Level level2 ) - { + + static public void logLevel(String logger, org.apache.log4j.Level level1, java.util.logging.Level level2) { if ( level1 != null ) org.apache.log4j.LogManager.getLogger(logger).setLevel(level1) ; if ( level2 != null ) java.util.logging.Logger.getLogger(logger).setLevel(level2) ; } - - /** Turn on a logger (all levels). - * Works for Log4j and Java logging as the logging provider to Apache common logging or slf4j. + + /** + * Turn on a logger (all levels). Works for Log4j and Java logging as the + * logging provider to Apache common logging or slf4j. */ - static public void enable(Class<?> logger) - { + static public void enable(Class<? > logger) { org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.ALL) ; java.util.logging.Logger.getLogger(logger.getName()).setLevel(java.util.logging.Level.ALL) ; } - - /** Turn on a logger (all levels). - * Works for Log4j and Java logging as the logging provider to Apache common logging or slf4j. + + /** + * Turn on a logger (all levels). Works for Log4j and Java logging as the + * logging provider to Apache common logging or slf4j. */ - static public void disable(String logger) - { + static public void disable(String logger) { org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.OFF) ; java.util.logging.Logger.getLogger(logger).setLevel(java.util.logging.Level.OFF) ; } - /** Turn on a logger (all levels). - * Works for Log4j and Java logging as the logging provider to Apache common logging or slf4j. + + /** + * Turn on a logger (all levels). Works for Log4j and Java logging as the + * logging provider to Apache common logging or slf4j. */ - static public void disable(Class<?> logger) - { + static public void disable(Class<? > logger) { org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.OFF) ; java.util.logging.Logger.getLogger(logger.getName()).setLevel(java.util.logging.Level.OFF) ; } - /** Set to warning level. - * Works for Log4j and Java logging as the logging provider to Apache common logging or slf4j. + /** + * Set to warning level. Works for Log4j and Java logging as the logging + * provider to Apache common logging or slf4j. */ - static public void setWarn(String logger) - { + static public void setWarn(String logger) { org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.WARN) ; java.util.logging.Logger.getLogger(logger).setLevel(java.util.logging.Level.WARNING) ; } - - /** Set to warning level. - * Works for Log4j and Java logging as the logging provider to Apache common logging or slf4j. + + /** + * Set to warning level. Works for Log4j and Java logging as the logging + * provider to Apache common logging or slf4j. */ - static public void setWarn(Class<?> logger) - { + static public void setWarn(Class<? > logger) { org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.WARN) ; java.util.logging.Logger.getLogger(logger.getName()).setLevel(java.util.logging.Level.WARNING) ; } - - /** Set to error level. - * Works for Log4j and Java logging as the logging provider to Apache common logging or slf4j. + + /** + * Set to error level. Works for Log4j and Java logging as the logging + * provider to Apache common logging or slf4j. */ - static public void setError(String logger) - { + static public void setError(String logger) { org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.ERROR) ; java.util.logging.Logger.getLogger(logger).setLevel(java.util.logging.Level.SEVERE) ; } - - /** Set to error level. - * Works for Log4j and Java logging as the logging provider to Apache common logging or slf4j. + + /** + * Set to error level. Works for Log4j and Java logging as the logging + * provider to Apache common logging or slf4j. */ - static public void setError(Class<?> logger) - { + static public void setError(Class<? > logger) { org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.ERROR) ; java.util.logging.Logger.getLogger(logger.getName()).setLevel(java.util.logging.Level.SEVERE) ; } - - /** Set to infomation level. - * Works for Log4j and Java logging as the logging provider to Apache common logging or slf4j. + + /** + * Set to infomation level. Works for Log4j and Java logging as the logging + * provider to Apache common logging or slf4j. */ - static public void setInfo(String logger) - { + static public void setInfo(String logger) { org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.INFO) ; java.util.logging.Logger.getLogger(logger).setLevel(java.util.logging.Level.INFO) ; } - /** Set to infomation level. - * Works for Log4j and Java logging as the logging provider to Apache common logging or slf4j. + /** + * Set to infomation level. Works for Log4j and Java logging as the logging + * provider to Apache common logging or slf4j. */ - static public void setInfo(Class<?> logger) - { + static public void setInfo(Class<? > logger) { org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.INFO) ; java.util.logging.Logger.getLogger(logger.getName()).setLevel(java.util.logging.Level.INFO) ; } - - /** Set logging + + /** + * Set logging * <ol> * <li>Check for -Dlog4j.configuration.</li> * <li>Looks for log4j.properties file in current directory.</li> * </ol> * Return true if we think Log4J is not initialized. */ - - public static boolean setLog4j() - { - if ( System.getProperty("log4j.configuration") == null ) - { + + public static boolean setLog4j() { + if ( System.getProperty("log4j.configuration") == null ) { String fn = "log4j.properties" ; File f = new File(fn) ; - if ( f.exists() ) - System.setProperty("log4j.configuration", "file:"+fn) ; + if ( f.exists() ) + System.setProperty("log4j.configuration", "file:" + fn) ; } - - return (System.getProperty("log4j.configuration") != null ) ; + + return (System.getProperty("log4j.configuration") != null) ; } - + /** Set log4j properties (XML or properties file) */ - public static void setLog4j(String filename) - { - if ( filename.toLowerCase().endsWith(".xml")) + public static void setLog4j(String filename) { + if ( filename.toLowerCase().endsWith(".xml") ) DOMConfigurator.configure(filename) ; else PropertyConfigurator.configure(filename) ; } - - private static String log4Jsetup = StrUtils.strjoinNL( - "## Plain output to stdout" - , "log4j.appender.jena.plain=org.apache.log4j.ConsoleAppender" - , "log4j.appender.jena.plain.target=System.out" - , "log4j.appender.jena.plain.layout=org.apache.log4j.PatternLayout" - , "log4j.appender.jena.plain.layout.ConversionPattern=%m%n" - - , "## Plain output with level, to stderr" - , "log4j.appender.jena.plainlevel=org.apache.log4j.ConsoleAppender" - , "log4j.appender.jena.plainlevel.target=System.err" - , "log4j.appender.jena.plainlevel.layout=org.apache.log4j.PatternLayout" - , "log4j.appender.jena.plainlevel.layout.ConversionPattern=%-5p %m%n" - - , "## Everything" - , "log4j.rootLogger=INFO, jena.plainlevel" - , "log4j.logger.com.hp.hpl.jena=WARN" - , "log4j.logger.org.openjena=WARN" - , "log4j.logger.org.apache.jena=WARN" - , "log4j.logger.org.apache.jena.tdb.loader=INFO" - - , "## Parser output" - , "log4j.additivity."+SysRIOT.riotLoggerName+"=false" - , "log4j.logger."+SysRIOT.riotLoggerName+"=INFO, jena.plainlevel " - ) ; - - /** Set logging, suitable for a command line application. + + private static String log4Jsetup = StrUtils.strjoinNL("## Plain output to stdout", + "log4j.appender.jena.plain=org.apache.log4j.ConsoleAppender", + "log4j.appender.jena.plain.target=System.out", + "log4j.appender.jena.plain.layout=org.apache.log4j.PatternLayout", + "log4j.appender.jena.plain.layout.ConversionPattern=%m%n" + + , + "## Plain output with level, to stderr", + "log4j.appender.jena.plainlevel=org.apache.log4j.ConsoleAppender", + "log4j.appender.jena.plainlevel.target=System.err", + "log4j.appender.jena.plainlevel.layout=org.apache.log4j.PatternLayout", + "log4j.appender.jena.plainlevel.layout.ConversionPattern=%-5p %m%n" + + , "## Everything", "log4j.rootLogger=INFO, jena.plainlevel", + "log4j.logger.com.hp.hpl.jena=WARN", + "log4j.logger.org.openjena=WARN", + "log4j.logger.org.apache.jena=WARN", + "log4j.logger.org.apache.jena.tdb.loader=INFO" + + , "## Parser output", "log4j.additivity." + + SysRIOT.riotLoggerName + "=false", + "log4j.logger." + SysRIOT.riotLoggerName + + "=INFO, jena.plainlevel ") ; + + /** + * Set logging, suitable for a command line application. * <ol> * <li>Check for -Dlog4j.configuration.</li> * <li>Looks for log4j.properties file in current directory.</li> * <li>Sets log4j using an internal configuration.</li> * </ol> */ - public static void setCmdLogging() - { - setCmdLogging(log4Jsetup) ; + public static void setCmdLogging() { + setCmdLogging(log4Jsetup) ; } - - /** Set logging, suitable for a command line application. + + /** + * Set logging, suitable for a command line application. * <ol> * <li>Check for -Dlog4j.configuration.</li> * <li>Looks for log4j.properties file in current directory.</li> * <li>Sets log4j using the provided default configuration.</li> * </ol> - * T + * T */ - public static void setCmdLogging(String defaultConfig) - { + public static void setCmdLogging(String defaultConfig) { if ( !setLog4j() ) resetLogging(log4Jsetup) ; } - - public static void resetLogging(String config) - { + public static void resetLogging(String config) { Properties p = new Properties() ; InputStream in = new ByteArrayInputStream(StrUtils.asUTF8bytes(config)) ; - try { p.load(in) ; } catch (IOException ex) {} + try { + p.load(in) ; + } catch (IOException ex) {} PropertyConfigurator.configure(p) ; System.setProperty("log4j.configuration", "set") ; } - - //---- java.util.logging - because that's always present. - static String defaultProperties = - StrUtils.strjoinNL( - // Handlers - output - // All (comma separated) - //"handlers=java.util.logging.ConsoleHandler,org.openjena.atlas.logging.java.ConsoleHandlerStdout", - - // Atlas. - "handlers=org.openjena.atlas.logging.java.ConsoleHandlerStdout" - , "org.openjena.atlas.logging.java.ConsoleHandlerStdout.level=INFO" - , "java.util.logging.ConsoleHandler.formatter=atlas.logging.java.TextFormatter" - - // Provided by the JRE - //"handlers=java.util.logging.ConsoleHandler" , - //, "java.util.logging.ConsoleHandler.level=INFO", - - ) ; - - - public static void setJavaLogging() - { + + // ---- java.util.logging - because that's always present. + static String defaultProperties = StrUtils.strjoinNL( + // Handlers - output + // All (comma + // separated) + // "handlers=java.util.logging.ConsoleHandler,org.openjena.atlas.logging.java.ConsoleHandlerStdout", + + // Atlas. + "handlers=org.openjena.atlas.logging.java.ConsoleHandlerStdout", + "org.openjena.atlas.logging.java.ConsoleHandlerStdout.level=INFO", + "java.util.logging.ConsoleHandler.formatter=atlas.logging.java.TextFormatter" + + // Provided by the JRE + // "handlers=java.util.logging.ConsoleHandler" + // , + // , + // "java.util.logging.ConsoleHandler.level=INFO", + + ) ; + + public static void setJavaLogging() { setJavaLogging("logging.properties") ; } - - - public static void setJavaLogging(String file) - { - try - { + + public static void setJavaLogging(String file) { + try { InputStream details = new FileInputStream(file) ; java.util.logging.LogManager.getLogManager().readConfiguration(details) ; - } catch (Exception ex) { throw new AtlasException(ex) ; } + } catch (Exception ex) { + throw new AtlasException(ex) ; + } } - - public static void setJavaLoggingDft() - { - try - { + + public static void setJavaLoggingDft() { + try { InputStream details = new ByteArrayInputStream(defaultProperties.getBytes("UTF-8")) ; java.util.logging.LogManager.getLogManager().readConfiguration(details) ; - - } catch (Exception ex) { throw new AtlasException(ex) ; } + + } catch (Exception ex) { + throw new AtlasException(ex) ; + } } }