| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | using Google.Apis.Util; |
| | using System; |
| | using System.Globalization; |
| |
|
| | namespace Google.Apis.Logging |
| | { |
| | |
| | |
| | |
| | public abstract class BaseLogger : ILogger |
| | { |
| | |
| | private const string DateTimeFormatString = "yyyy-MM-dd HH:mm:ss.ffffff"; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | protected BaseLogger(LogLevel minimumLogLevel, IClock clock, Type forType) |
| | { |
| | MinimumLogLevel = minimumLogLevel; |
| | IsDebugEnabled = minimumLogLevel <= LogLevel.Debug; |
| | IsInfoEnabled = minimumLogLevel <= LogLevel.Info; |
| | IsWarningEnabled = minimumLogLevel <= LogLevel.Warning; |
| | IsErrorEnabled = minimumLogLevel <= LogLevel.Error; |
| | Clock = clock ?? SystemClock.Default; |
| | LoggerForType = forType; |
| | if (forType != null) |
| | { |
| | var namespaceStr = forType.Namespace ?? ""; |
| | if (namespaceStr.Length > 0) |
| | { |
| | namespaceStr += "."; |
| | } |
| | _loggerForTypeString = namespaceStr + forType.Name + " "; |
| | } |
| | else |
| | { |
| | _loggerForTypeString = ""; |
| | } |
| | } |
| |
|
| | private readonly string _loggerForTypeString; |
| |
|
| | |
| | |
| | |
| | public IClock Clock { get; } |
| |
|
| | |
| | |
| | |
| | public Type LoggerForType { get; } |
| |
|
| | |
| | |
| | |
| | public LogLevel MinimumLogLevel { get; } |
| |
|
| | |
| | |
| | |
| | public bool IsDebugEnabled { get; } |
| |
|
| | |
| | |
| | |
| | public bool IsInfoEnabled { get; } |
| |
|
| | |
| | |
| | |
| | public bool IsWarningEnabled { get; } |
| |
|
| | |
| | |
| | |
| | public bool IsErrorEnabled { get; } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | protected abstract ILogger BuildNewLogger(Type type); |
| |
|
| | |
| | public ILogger ForType<T>() => ForType(typeof(T)); |
| |
|
| | |
| | public ILogger ForType(Type type) => type == LoggerForType ? this : BuildNewLogger(type); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | protected abstract void Log(LogLevel logLevel, string formattedMessage); |
| |
|
| | private string FormatLogEntry(string severityString, string message, params object[] formatArgs) |
| | { |
| | var msg = string.Format(message, formatArgs); |
| | var when = Clock.UtcNow.ToString(DateTimeFormatString, CultureInfo.InvariantCulture); |
| | |
| | return $"{severityString}{when} {_loggerForTypeString}{msg}"; |
| | } |
| |
|
| | |
| | public void Debug(string message, params object[] formatArgs) |
| | { |
| | if (IsDebugEnabled) |
| | { |
| | Log(LogLevel.Debug, FormatLogEntry("D", message, formatArgs)); |
| | } |
| | } |
| |
|
| | |
| | public void Info(string message, params object[] formatArgs) |
| | { |
| | if (IsInfoEnabled) |
| | { |
| | Log(LogLevel.Info, FormatLogEntry("I", message, formatArgs)); |
| | } |
| | } |
| |
|
| | |
| | public void Warning(string message, params object[] formatArgs) |
| | { |
| | if (IsWarningEnabled) |
| | { |
| | Log(LogLevel.Warning, FormatLogEntry("W", message, formatArgs)); |
| | } |
| | } |
| |
|
| | |
| | public void Error(Exception exception, string message, params object[] formatArgs) |
| | { |
| | if (IsErrorEnabled) |
| | { |
| | Log(LogLevel.Error, $"{FormatLogEntry("E", message, formatArgs)} {exception}"); |
| | } |
| | } |
| |
|
| | |
| | public void Error(string message, params object[] formatArgs) |
| | { |
| | if (IsErrorEnabled) |
| | { |
| | Log(LogLevel.Error, FormatLogEntry("E", message, formatArgs)); |
| | } |
| | } |
| |
|
| | } |
| | } |
| |
|