Last active
December 14, 2015 04:28
-
-
Save wsky/5027961 to your computer and use it in GitHub Desktop.
Java/C# Logger class, usual used in framework/lib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary>log write to console | |
/// </summary> | |
public class DefaultLogger : ILog | |
{ | |
public string Name { get; private set; } | |
public bool IsDebugEnabled { get; private set; } | |
public bool IsInfoEnabled { get; private set; } | |
public bool IsWarnEnabled { get; private set; } | |
public bool IsErrorEnabled { get; private set; } | |
public bool IsFatalEnabled { get; private set; } | |
public DefaultLogger(string name | |
, bool isDebugEnabled | |
, bool isInfoEnabled | |
, bool isWarnEnabled | |
, bool isErrorEnabled | |
, bool isFatalEnabled) | |
{ | |
this.Name = name; | |
this.IsDebugEnabled = isDebugEnabled; | |
this.IsInfoEnabled = isInfoEnabled; | |
this.IsWarnEnabled = isWarnEnabled; | |
this.IsErrorEnabled = isErrorEnabled; | |
this.IsFatalEnabled = isFatalEnabled; | |
} | |
public void Debug(object message) | |
{ | |
this.Write(Level.DEBUG, message); | |
} | |
public void DebugFormat(string format, params object[] args) | |
{ | |
this.WriteFormat(Level.DEBUG, format, args); | |
} | |
public void Debug(object message, Exception exception) | |
{ | |
this.WriteFormat(Level.DEBUG, "{0}\n{1}\n{2}", message, exception.Message, exception.StackTrace); | |
} | |
public void Info(object message) | |
{ | |
this.Write(Level.INFO, message); | |
} | |
public void InfoFormat(string format, params object[] args) | |
{ | |
this.WriteFormat(Level.INFO, format, args); | |
} | |
public void Info(object message, Exception exception) | |
{ | |
this.WriteFormat(Level.INFO, "{0}\n{1}\n{2}", message, exception.Message, exception.StackTrace); | |
} | |
public void Warn(object message) | |
{ | |
this.Write(Level.WARN, message); | |
} | |
public void WarnFormat(string format, params object[] args) | |
{ | |
this.WriteFormat(Level.WARN, format, args); | |
} | |
public void Warn(object message, Exception exception) | |
{ | |
this.WriteFormat(Level.WARN, "{0}\n{1}\n{2}", message, exception.Message, exception.StackTrace); | |
} | |
public void Error(object message) | |
{ | |
this.Write(Level.ERROR, message); | |
} | |
public void ErrorFormat(string format, params object[] args) | |
{ | |
this.WriteFormat(Level.ERROR, format, args); | |
} | |
public void Error(object message, Exception exception) | |
{ | |
this.WriteFormat(Level.ERROR, "{0}\n{1}\n{2}", message, exception.Message, exception.StackTrace); | |
} | |
public void Fatal(object message) | |
{ | |
this.Write(Level.FATAL, message); | |
} | |
public void FatalFormat(string format, params object[] args) | |
{ | |
this.WriteFormat(Level.FATAL, format, args); | |
} | |
public void Fatal(object message, Exception exception) | |
{ | |
this.WriteFormat(Level.FATAL, "{0}\n{1}\n{2}", message, exception.Message, exception.StackTrace); | |
} | |
private void WriteFormat(Level level, string format, params object[] args) | |
{ | |
this.Write(level, string.Format(format, args)); | |
} | |
private void Write(Level level, object message) | |
{ | |
Console.WriteLine("[{0}] [{1}] [{2}] - {3}" | |
, level | |
, this.Name | |
, Thread.CurrentThread.Name | |
, message); | |
} | |
enum Level | |
{ | |
DEBUG, INFO, WARN, ERROR, FATAL | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DefaultLogger implements Logger { | |
private String type; | |
private boolean isDebugEnabled; | |
private boolean isInfoEnabled; | |
private boolean isWarnEnabled; | |
private boolean isErrorEnabled; | |
private boolean isFatalEnabled; | |
public DefaultLogger(String type, | |
boolean isDebugEnabled, | |
boolean isInfoEnabled, | |
boolean isWarnEnabled, | |
boolean isErrorEnabled, | |
boolean isFatalEnabled) { | |
this.type = type; | |
this.isDebugEnabled = isDebugEnabled; | |
this.isInfoEnabled = isInfoEnabled; | |
this.isWarnEnabled = isWarnEnabled; | |
this.isErrorEnabled = isErrorEnabled; | |
this.isFatalEnabled = isFatalEnabled; | |
} | |
@Override | |
public boolean isDebugEnabled() { | |
return this.isDebugEnabled; | |
} | |
@Override | |
public boolean isInfoEnabled() { | |
return this.isInfoEnabled; | |
} | |
@Override | |
public boolean isWarnEnabled() { | |
return this.isWarnEnabled; | |
} | |
@Override | |
public boolean isErrorEnabled() { | |
return this.isErrorEnabled; | |
} | |
@Override | |
public boolean isFatalEnabled() { | |
return this.isFatalEnabled; | |
} | |
@Override | |
public void debug(String message) { | |
System.out.println(String.format("[DEBUG] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), message)); | |
} | |
@Override | |
public void debug(Throwable exception) { | |
System.out.println(String.format("[DEBUG] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), exception)); | |
} | |
@Override | |
public void debug(String message, Throwable exception) { | |
System.out.println(String.format("[DEBUG] [%s] [%s] - %s %s", this.type, Thread.currentThread().getName(), message, exception)); | |
} | |
@Override | |
public void debug(String format, Object... args) { | |
System.out.println(String.format("[DEBUG] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), String.format(format, args))); | |
} | |
@Override | |
public void info(String message) { | |
System.out.println(String.format("[INFO] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), message)); | |
} | |
@Override | |
public void info(Throwable exception) { | |
System.out.println(String.format("[INFO] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), exception)); | |
} | |
@Override | |
public void info(String message, Throwable exception) { | |
System.out.println(String.format("[INFO] [%s] [%s] - %s %s", this.type, Thread.currentThread().getName(), message, exception)); | |
} | |
@Override | |
public void info(String format, Object... args) { | |
System.out.println(String.format("[INFO] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), String.format(format, args))); | |
} | |
@Override | |
public void warn(String message) { | |
System.out.println(String.format("[WARN] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), message)); | |
} | |
@Override | |
public void warn(Throwable exception) { | |
System.out.println(String.format("[WARN] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), exception)); | |
} | |
@Override | |
public void warn(String message, Throwable exception) { | |
System.out.println(String.format("[WARN] [%s] [%s] - %s %s", this.type, Thread.currentThread().getName(), message, exception)); | |
} | |
@Override | |
public void warn(String format, Object... args) { | |
System.out.println(String.format("[WARN] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), String.format(format, args))); | |
} | |
@Override | |
public void error(String message) { | |
System.err.println(String.format("[ERROR] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), message)); | |
} | |
@Override | |
public void error(Throwable exception) { | |
System.err.println(String.format("[ERROR] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), exception)); | |
exception.printStackTrace(); | |
} | |
@Override | |
public void error(String message, Throwable exception) { | |
System.err.println(String.format("[ERROR] [%s] [%s] - %s %s", this.type, Thread.currentThread().getName(), message, exception)); | |
exception.printStackTrace(); | |
} | |
@Override | |
public void error(String format, Object... args) { | |
System.err.println(String.format("[ERROR] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), String.format(format, args))); | |
} | |
@Override | |
public void fatal(String message) { | |
System.err.println(String.format("[FATAL] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), message)); | |
} | |
@Override | |
public void fatal(Throwable exception) { | |
System.err.println(String.format("[FATAL] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), exception)); | |
exception.printStackTrace(); | |
} | |
@Override | |
public void fatal(String message, Throwable exception) { | |
System.err.println(String.format("[FATAL] [%s] [%s] - %s %s", this.type, Thread.currentThread().getName(), message, exception)); | |
exception.printStackTrace(); | |
} | |
@Override | |
public void fatal(String format, Object... args) { | |
System.err.println(String.format("[FATAL] [%s] [%s] - %s", this.type, Thread.currentThread().getName(), String.format(format, args))); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DefaultLoggerFactory : ILoggerFactory | |
{ | |
private static ILoggerFactory _default; | |
public static ILoggerFactory Default | |
{ | |
get | |
{ | |
return _default ?? (_default = new DefaultLoggerFactory(true, true, true, true, true)); | |
} | |
} | |
public bool IsDebugEnabled { get; private set; } | |
public bool IsErrorEnabled { get; private set; } | |
public bool IsFatalEnabled { get; private set; } | |
public bool IsInfoEnabled { get; private set; } | |
public bool IsWarnEnabled { get; private set; } | |
public DefaultLoggerFactory(bool isDebugEnabled | |
, bool isErrorEnabled | |
, bool isFatalEnabled | |
, bool isInfoEnabled | |
, bool isWarnEnabled) | |
{ | |
this.IsDebugEnabled = isDebugEnabled; | |
this.IsInfoEnabled = isInfoEnabled; | |
this.IsWarnEnabled = isWarnEnabled; | |
this.IsErrorEnabled = isErrorEnabled; | |
this.IsFatalEnabled = isFatalEnabled; | |
} | |
public ILog Create(string name) | |
{ | |
return new DefaultLogger(name | |
, this.IsDebugEnabled | |
, this.IsInfoEnabled | |
, this.IsWarnEnabled | |
, this.IsErrorEnabled | |
, this.IsFatalEnabled); | |
} | |
public ILog Create(Type type) | |
{ | |
return new DefaultLogger(type.FullName | |
, this.IsDebugEnabled | |
, this.IsInfoEnabled | |
, this.IsWarnEnabled | |
, this.IsErrorEnabled | |
, this.IsFatalEnabled); | |
} | |
public ILog Create(object obj) | |
{ | |
return new DefaultLogger(obj.GetType().FullName | |
, this.IsDebugEnabled | |
, this.IsInfoEnabled | |
, this.IsWarnEnabled | |
, this.IsErrorEnabled | |
, this.IsFatalEnabled); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DefaultLoggerFactory implements LoggerFactory { | |
private static LoggerFactory _default; | |
static { | |
setDefault(false, true, true, true, true); | |
} | |
public static LoggerFactory getDefault() { | |
return _default; | |
} | |
public static void setDefault(boolean isDebugEnabled, | |
boolean isInfoEnabled, | |
boolean isWarnEnabled, | |
boolean isErrorEnabled, | |
boolean isFatalEnabled) { | |
_default = new DefaultLoggerFactory(isDebugEnabled, | |
isInfoEnabled, | |
isWarnEnabled, | |
isErrorEnabled, | |
isFatalEnabled); | |
} | |
private boolean isDebugEnabled; | |
private boolean isInfoEnabled; | |
private boolean isWarnEnabled; | |
private boolean isErrorEnabled; | |
private boolean isFatalEnabled; | |
public DefaultLoggerFactory() { | |
this(false, true, true, true, true); | |
} | |
public DefaultLoggerFactory(boolean isDebugEnabled, | |
boolean isInfoEnabled, | |
boolean isWarnEnabled, | |
boolean isErrorEnabled, | |
boolean isFatalEnabled) { | |
this.isDebugEnabled = isDebugEnabled; | |
this.isInfoEnabled = isInfoEnabled; | |
this.isWarnEnabled = isWarnEnabled; | |
this.isErrorEnabled = isErrorEnabled; | |
this.isFatalEnabled = isFatalEnabled; | |
} | |
@Override | |
public Logger create(String type) { | |
return new DefaultLogger(type, | |
this.isDebugEnabled, | |
this.isInfoEnabled, | |
this.isWarnEnabled, | |
this.isErrorEnabled, | |
this.isFatalEnabled); | |
} | |
@Override | |
public Logger create(Class<?> type) { | |
return new DefaultLogger(type.getSimpleName(), | |
this.isDebugEnabled, | |
this.isInfoEnabled, | |
this.isWarnEnabled, | |
this.isErrorEnabled, | |
this.isFatalEnabled); | |
} | |
@Override | |
public Logger create(Object object) { | |
return new DefaultLogger(object.getClass().getSimpleName(), | |
this.isDebugEnabled, | |
this.isInfoEnabled, | |
this.isWarnEnabled, | |
this.isErrorEnabled, | |
this.isFatalEnabled); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ILog | |
{ | |
bool IsDebugEnabled { get; } | |
bool IsErrorEnabled { get; } | |
bool IsFatalEnabled { get; } | |
bool IsInfoEnabled { get; } | |
bool IsWarnEnabled { get; } | |
void Debug(object message); | |
void DebugFormat(string format, params object[] args); | |
void Debug(object message, Exception exception); | |
void Info(object message); | |
void InfoFormat(string format, params object[] args); | |
void Info(object message, Exception exception); | |
void Warn(object message); | |
void WarnFormat(string format, params object[] args); | |
void Warn(object message, Exception exception); | |
void Error(object message); | |
void ErrorFormat(string format, params object[] args); | |
void Error(object message, Exception exception); | |
void Fatal(object message); | |
void FatalFormat(string format, params object[] args); | |
void Fatal(object message, Exception exception); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary>logger provider | |
/// </summary> | |
public interface ILoggerFactory | |
{ | |
ILog Create(string name); | |
ILog Create(Type type); | |
ILog Create(object obj); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Logger { | |
public boolean isDebugEnabled(); | |
public boolean isInfoEnabled(); | |
public boolean isWarnEnabled(); | |
public boolean isErrorEnabled(); | |
public boolean isFatalEnabled(); | |
public void debug(String message); | |
public void debug(Throwable exception); | |
public void debug(String message, Throwable exception); | |
public void debug(String format, Object... args); | |
public void info(String message); | |
public void info(Throwable exception); | |
public void info(String message, Throwable exception); | |
public void info(String format, Object... args); | |
public void warn(String message); | |
public void warn(Throwable exception); | |
public void warn(String message, Throwable exception); | |
public void warn(String format, Object... args); | |
public void error(String message); | |
public void error(Throwable exception); | |
public void error(String message, Throwable exception); | |
public void error(String format, Object... args); | |
public void fatal(String message); | |
public void fatal(Throwable exception); | |
public void fatal(String message, Throwable exception); | |
public void fatal(String format, Object... args); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Type; | |
public interface LoggerFactory { | |
public Logger create(String type); | |
public Logger create(Class<?> type); | |
public Logger create(Object object); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment