From 8652a7e749a07e2118d5c7baf4a5eb0208b6037a Mon Sep 17 00:00:00 2001 From: mdb Date: Sat, 27 Nov 2004 18:07:58 +0000 Subject: [PATCH] A bridge between samskivert's (old old old) logging facilities and Java's (new new new) built-in logging system. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1536 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../samskivert/util/LoggingLogProvider.java | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 projects/samskivert/src/java/com/samskivert/util/LoggingLogProvider.java diff --git a/projects/samskivert/src/java/com/samskivert/util/LoggingLogProvider.java b/projects/samskivert/src/java/com/samskivert/util/LoggingLogProvider.java new file mode 100644 index 00000000..d22be5e0 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/util/LoggingLogProvider.java @@ -0,0 +1,105 @@ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001-2004 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.util; + +import java.util.HashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Configures the samskivert logging routines to use the Java logging + * facilities for logging. This provides a usable temporary bridge until + * things that use the (now legacy) samskivert logging routines can be + * converted to use Java's logging facilities directly. + */ +public class LoggingLogProvider + implements LogProvider +{ + // documentation inherited from interface + public void log (int level, String moduleName, String message) + { + getLogger(moduleName).log(getLevel(level), message); + } + + // documentation inherited from interface + public void logStackTrace (int level, String moduleName, Throwable t) + { + getLogger(moduleName).log(getLevel(level), "", t); + } + + // documentation inherited from interface + public void setLevel (String moduleName, int level) + { + getLogger(moduleName).setLevel(getLevel(level)); + } + + // documentation inherited from interface + public void setLevel (int level) + { + getLogger(null).setLevel(getLevel(level)); + } + + // documentation inherited from interface + public int getLevel (String moduleName) + { + return getLevel(getLogger(moduleName).getLevel()); + } + + // documentation inherited from interface + public int getLevel () + { + return getLevel(getLogger(null).getLevel()); + } + + protected final Logger getLogger (String moduleName) + { + Logger logger = Logger.global; + if (!StringUtil.blank(moduleName)) { + logger = (Logger)_loggers.get(moduleName); + if (logger == null) { + _loggers.put(moduleName, + logger = Logger.getLogger("moduleName")); + } + } + return logger; + } + + protected static final Level getLevel (int level) + { + switch (level) { + case Log.DEBUG: return Level.FINE; + case Log.WARNING: return Level.WARNING; + default: + case Log.INFO: return Level.INFO; + } + } + + protected static final int getLevel (Level level) + { + if (level == Level.WARNING) { + return Log.WARNING; + } else if (level == Level.INFO) { + return Log.INFO; + } else { + return Log.DEBUG; + } + } + + protected HashMap _loggers = new HashMap(); +}