// edtFTPnet
//
// Copyright (C) 2004 Enterprise Distributed Technologies Ltd
//
// www.enterprisedt.com
//
// 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
//
// Bug fixes, suggestions and comments should posted on
// http://www.enterprisedt.com/forums/index.php
//
// Change Log:
//
// $Log: Logger.cs,v $
// Revision 1.6 2004/11/13 18:20:52 bruceb
// clear appenders/loggers in shutdown
//
// Revision 1.5 2004/11/06 11:15:24 bruceb
// namespace tidying up
//
// Revision 1.4 2004/10/29 09:42:30 bruceb
// removed /// from file headers
//
//
//
using System;
using System.Globalization;
using System.Collections;
using System.Configuration;
namespace EnterpriseDT.Util.Debug
{
///
/// Logger class that mimics log4net Logger class
///
/// Bruce Blackshaw
///
/// $LastChangedRevision$
///
public class Logger
{
///
/// Set all loggers to this level
///
public static Level CurrentLevel
{
set
{
globalLevel = value;
}
get
{
return globalLevel;
}
}
///
/// Is debug logging enabled?
///
/// true if enabled
///
virtual public bool DebugEnabled
{
get
{
return IsEnabledFor(Level.DEBUG);
}
}
/// Is info logging enabled for the supplied level?
///
///
/// true if enabled
///
virtual public bool InfoEnabled
{
get
{
return IsEnabledFor(Level.INFO);
}
}
/// Level of all loggers
private static Level globalLevel;
/// Date format
private static readonly string format = "d MMM yyyy HH:mm:ss.fff";
/// Hash of all loggers that exist
private static Hashtable loggers = Hashtable.Synchronized(new Hashtable(10));
/// Vector of all appenders
private static ArrayList appenders = ArrayList.Synchronized(new ArrayList(2));
/// Timestamp
private DateTime ts;
/// Class name for this logger
private string clazz;
///
/// Constructor
///
///
/// class this logger is for
///
private Logger(string clazz)
{
this.clazz = clazz;
}
/// Get a logger for the supplied class
///
///
/// full class name
///
/// logger for class
///
public static Logger GetLogger(System.Type clazz)
{
return GetLogger(clazz.FullName);
}
///
/// Get a logger for the supplied class
///
/// full class name
///
/// logger for class
///
public static Logger GetLogger(string clazz)
{
Logger logger = (Logger) loggers[clazz];
if (logger == null)
{
logger = new Logger(clazz);
loggers[clazz] = logger;
}
return logger;
}
///
/// Add an appender to our list
///
///
/// new appender to add
///
public static void AddAppender(Appender newAppender)
{
appenders.Add(newAppender);
}
/// Close and remove all appenders and loggers
public static void Shutdown()
{
for (int i = 0; i < appenders.Count; i++)
{
Appender a = (Appender) appenders[i];
a.Close();
}
loggers.Clear();
appenders.Clear();
}
/// Log a message
///
///
/// log level
///
/// message to log
///
/// throwable object
///
public virtual void Log(Level level, string message, Exception t)
{
if (globalLevel.IsGreaterOrEqual(level))
OurLog(level, message, t);
}
///
/// Log a message to our logging system
///
/// log level
///
/// message to log
///
/// throwable object
///
private void OurLog(Level level, string message, Exception t)
{
ts = DateTime.Now;
string stamp = ts.ToString(format, CultureInfo.CurrentCulture.DateTimeFormat);
System.Text.StringBuilder buf = new System.Text.StringBuilder(level.ToString());
buf.Append(" [").Append(clazz).Append("] ").Append(stamp).Append(" : ").Append(message);
if (appenders.Count == 0)
{
// by default to stdout
System.Console.Out.WriteLine(buf.ToString());
if (t != null)
{
System.Console.Out.WriteLine(t.StackTrace);
}
}
else
{
for (int i = 0; i < appenders.Count; i++)
{
Appender a = (Appender) appenders[i];
a.Log(buf.ToString());
if (t != null)
{
a.Log(t);
}
}
}
}
/// Log an info level message
///
///
/// message to log
///
public virtual void Info(string message)
{
Log(Level.INFO, message, null);
}
/// Log an info level message
///
///
/// message to log
///
/// throwable object
///
public virtual void Info(string message, Exception t)
{
Log(Level.INFO, message, t);
}
/// Log a warning level message
///
///
/// message to log
///
public virtual void Warn(string message)
{
Log(Level.WARN, message, null);
}
/// Log a warning level message
///
///
/// message to log
///
/// throwable object
///
public virtual void Warn(string message, Exception t)
{
Log(Level.WARN, message, t);
}
/// Log an error level message
///
///
/// message to log
///
public virtual void Error(string message)
{
Log(Level.ERROR, message, null);
}
/// Log an error level message
///
///
/// message to log
///
/// throwable object
///
public virtual void Error(string message, Exception t)
{
Log(Level.ERROR, message, t);
}
/// Log a fatal level message
///
///
/// message to log
///
public virtual void Fatal(string message)
{
Log(Level.FATAL, message, null);
}
/// Log a fatal level message
///
///
/// message to log
///
/// throwable object
///
public virtual void Fatal(string message, Exception t)
{
Log(Level.FATAL, message, t);
}
/// Log a debug level message
///
///
/// message to log
///
public virtual void Debug(string message)
{
Log(Level.DEBUG, message, null);
}
/// Log a debug level message
///
///
/// message to log
///
/// throwable object
///
public virtual void Debug(string message, Exception t)
{
Log(Level.DEBUG, message, t);
}
/// Is logging enabled for the supplied level?
///
///
/// level to test for
///
/// true if enabled
///
public virtual bool IsEnabledFor(Level level)
{
if (globalLevel.IsGreaterOrEqual(level))
return true;
return false;
}
/// Determine the logging level
static Logger()
{
{
string level = ConfigurationSettings.AppSettings["edtftp.log.level"];
if (level != null)
globalLevel = Level.GetLevel(level);
else {
globalLevel = Level.OFF;
System.Console.Out.WriteLine("WARNING: 'edtftp.log.level' not found - logging switched off");
}
}
}
}
}