When json logging is enabled, log it to the console without color or additional timestamp, also log the messages with the correct log level. Remove the logs folder.
This commit is contained in:
parent
546b5450e7
commit
8246c7a862
3 changed files with 36 additions and 57 deletions
|
@ -20,44 +20,25 @@ namespace Geekbot.Core.Logger
|
||||||
};
|
};
|
||||||
Information(LogSource.Geekbot, "Using GeekbotLogger");
|
Information(LogSource.Geekbot, "Using GeekbotLogger");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Trace(LogSource source, string message, object extra = null)
|
public void Trace(LogSource source, string message, object extra = null)
|
||||||
{
|
=> _logger.Trace(CreateLogString("Trace", source, message, null, extra));
|
||||||
_logger.Trace(CreateLogString("Trace", source, message, null, extra));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Debug(LogSource source, string message, object extra = null)
|
public void Debug(LogSource source, string message, object extra = null)
|
||||||
{
|
=> _logger.Debug(CreateLogString("Debug", source, message, null, extra));
|
||||||
if (_logAsJson) _logger.Info(CreateLogString("Debug", source, message, null, extra));
|
|
||||||
else _logger.Debug(CreateLogString("Debug", source, message, null, extra));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Information(LogSource source, string message, object extra = null)
|
public void Information(LogSource source, string message, object extra = null)
|
||||||
{
|
=> _logger.Info(CreateLogString("Information", source, message, null, extra));
|
||||||
_logger.Info(CreateLogString("Information", source, message, null, extra));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Warning(LogSource source, string message, Exception stackTrace = null, object extra = null)
|
public void Warning(LogSource source, string message, Exception stackTrace = null, object extra = null)
|
||||||
{
|
=> _logger.Warn(CreateLogString("Warning", source, message, stackTrace, extra));
|
||||||
if (_logAsJson) _logger.Info(CreateLogString("Warning", source, message, stackTrace, extra));
|
|
||||||
else _logger.Warn(CreateLogString("Warning", source, message, stackTrace, extra));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Error(LogSource source, string message, Exception stackTrace, object extra = null)
|
public void Error(LogSource source, string message, Exception stackTrace, object extra = null)
|
||||||
{
|
=> _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra));
|
||||||
if (_logAsJson) _logger.Info(CreateLogString("Error", source, message, stackTrace, extra));
|
|
||||||
else _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra));
|
|
||||||
}
|
|
||||||
|
|
||||||
public NLog.Logger GetNLogger()
|
public NLog.Logger GetNLogger() => _logger;
|
||||||
{
|
|
||||||
return _logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool LogAsJson()
|
public bool LogAsJson() => _logAsJson;
|
||||||
{
|
|
||||||
return _logAsJson;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string CreateLogString(string type, LogSource source, string message, Exception stackTrace = null, object extra = null)
|
private string CreateLogString(string type, LogSource source, string message, Exception stackTrace = null, object extra = null)
|
||||||
{
|
{
|
||||||
|
@ -74,10 +55,10 @@ namespace Geekbot.Core.Logger
|
||||||
};
|
};
|
||||||
return JsonConvert.SerializeObject(logObject, Formatting.None, _serializerSettings);
|
return JsonConvert.SerializeObject(logObject, Formatting.None, _serializerSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (source != LogSource.Message) return $"[{source}] - {message}";
|
if (source != LogSource.Message) return $"[{source}] - {message}";
|
||||||
|
|
||||||
var m = (MessageDto) extra;
|
var m = (MessageDto) extra;
|
||||||
return $"[{source}] - [{m?.Guild.Name} - {m?.Channel.Name}] {m?.User.Name}: {m?.Message.Content}";
|
return $"[{source}] - [{m?.Guild.Name} - {m?.Channel.Name}] {m?.User.Name}: {m?.Message.Content}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,12 +12,13 @@ namespace Geekbot.Core.Logger
|
||||||
public static NLog.Logger CreateNLog(RunParameters runParameters)
|
public static NLog.Logger CreateNLog(RunParameters runParameters)
|
||||||
{
|
{
|
||||||
var config = new LoggingConfiguration();
|
var config = new LoggingConfiguration();
|
||||||
|
var minLevel = runParameters.Verbose ? LogLevel.Trace : LogLevel.Info;
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(runParameters.SumologicEndpoint))
|
if (!string.IsNullOrEmpty(runParameters.SumologicEndpoint))
|
||||||
{
|
{
|
||||||
Console.WriteLine("Logging Geekbot Logs to Sumologic");
|
Console.WriteLine("Logging Geekbot Logs to Sumologic");
|
||||||
config.LoggingRules.Add(
|
config.LoggingRules.Add(
|
||||||
new LoggingRule("*", LogLevel.Debug, LogLevel.Fatal,
|
new LoggingRule("*", minLevel, LogLevel.Fatal,
|
||||||
new SumoLogicTarget()
|
new SumoLogicTarget()
|
||||||
{
|
{
|
||||||
Url = runParameters.SumologicEndpoint,
|
Url = runParameters.SumologicEndpoint,
|
||||||
|
@ -27,11 +28,23 @@ namespace Geekbot.Core.Logger
|
||||||
OptimizeBufferReuse = true,
|
OptimizeBufferReuse = true,
|
||||||
Name = "Geekbot"
|
Name = "Geekbot"
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
else if (runParameters.LogJson)
|
||||||
|
{
|
||||||
|
config.LoggingRules.Add(
|
||||||
|
new LoggingRule("*", minLevel, LogLevel.Fatal,
|
||||||
|
new ConsoleTarget
|
||||||
|
{
|
||||||
|
Name = "Console",
|
||||||
|
Encoding = Encoding.UTF8,
|
||||||
|
Layout = "${message}"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var minLevel = runParameters.Verbose ? LogLevel.Trace : LogLevel.Info;
|
|
||||||
config.LoggingRules.Add(
|
config.LoggingRules.Add(
|
||||||
new LoggingRule("*", minLevel, LogLevel.Fatal,
|
new LoggingRule("*", minLevel, LogLevel.Fatal,
|
||||||
new ColoredConsoleTarget
|
new ColoredConsoleTarget
|
||||||
|
@ -39,27 +52,12 @@ namespace Geekbot.Core.Logger
|
||||||
Name = "Console",
|
Name = "Console",
|
||||||
Encoding = Encoding.UTF8,
|
Encoding = Encoding.UTF8,
|
||||||
Layout = "[${longdate} ${level:format=FirstCharacter}] ${message} ${exception:format=toString}"
|
Layout = "[${longdate} ${level:format=FirstCharacter}] ${message} ${exception:format=toString}"
|
||||||
})
|
}
|
||||||
);
|
)
|
||||||
|
);
|
||||||
config.LoggingRules.Add(
|
|
||||||
new LoggingRule("*", minLevel, LogLevel.Fatal,
|
|
||||||
new FileTarget
|
|
||||||
{
|
|
||||||
Name = "File",
|
|
||||||
Layout = "[${longdate} ${level}] ${message}",
|
|
||||||
Encoding = Encoding.UTF8,
|
|
||||||
LineEnding = LineEndingMode.Default,
|
|
||||||
MaxArchiveFiles = 30,
|
|
||||||
ArchiveNumbering = ArchiveNumberingMode.Date,
|
|
||||||
ArchiveEvery = FileArchivePeriod.Day,
|
|
||||||
ArchiveFileName = "./Logs/Archive/{#####}.log",
|
|
||||||
FileName = "./Logs/Geekbot.log"
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var loggerConfig = new LogFactory { Configuration = config };
|
var loggerConfig = new LogFactory {Configuration = config};
|
||||||
return loggerConfig.GetCurrentClassLogger();
|
return loggerConfig.GetCurrentClassLogger();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue