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");
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
if (_logAsJson) _logger.Info(CreateLogString("Debug", source, message, null, extra));
|
||||
else _logger.Debug(CreateLogString("Debug", source, message, null, extra));
|
||||
}
|
||||
|
||||
=> _logger.Debug(CreateLogString("Debug", source, message, null, extra));
|
||||
|
||||
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)
|
||||
{
|
||||
if (_logAsJson) _logger.Info(CreateLogString("Warning", source, message, stackTrace, extra));
|
||||
else _logger.Warn(CreateLogString("Warning", source, message, stackTrace, extra));
|
||||
}
|
||||
|
||||
=> _logger.Warn(CreateLogString("Warning", source, message, stackTrace, extra));
|
||||
|
||||
public void Error(LogSource source, string message, Exception stackTrace, object extra = null)
|
||||
{
|
||||
if (_logAsJson) _logger.Info(CreateLogString("Error", source, message, stackTrace, extra));
|
||||
else _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra));
|
||||
}
|
||||
=> _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra));
|
||||
|
||||
public NLog.Logger GetNLogger()
|
||||
{
|
||||
return _logger;
|
||||
}
|
||||
public NLog.Logger GetNLogger() => _logger;
|
||||
|
||||
public bool LogAsJson()
|
||||
{
|
||||
return _logAsJson;
|
||||
}
|
||||
public bool LogAsJson() => _logAsJson;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
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}";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,12 +12,13 @@ namespace Geekbot.Core.Logger
|
|||
public static NLog.Logger CreateNLog(RunParameters runParameters)
|
||||
{
|
||||
var config = new LoggingConfiguration();
|
||||
var minLevel = runParameters.Verbose ? LogLevel.Trace : LogLevel.Info;
|
||||
|
||||
if (!string.IsNullOrEmpty(runParameters.SumologicEndpoint))
|
||||
{
|
||||
Console.WriteLine("Logging Geekbot Logs to Sumologic");
|
||||
config.LoggingRules.Add(
|
||||
new LoggingRule("*", LogLevel.Debug, LogLevel.Fatal,
|
||||
new LoggingRule("*", minLevel, LogLevel.Fatal,
|
||||
new SumoLogicTarget()
|
||||
{
|
||||
Url = runParameters.SumologicEndpoint,
|
||||
|
@ -27,11 +28,23 @@ namespace Geekbot.Core.Logger
|
|||
OptimizeBufferReuse = true,
|
||||
Name = "Geekbot"
|
||||
})
|
||||
);
|
||||
);
|
||||
}
|
||||
else if (runParameters.LogJson)
|
||||
{
|
||||
config.LoggingRules.Add(
|
||||
new LoggingRule("*", minLevel, LogLevel.Fatal,
|
||||
new ConsoleTarget
|
||||
{
|
||||
Name = "Console",
|
||||
Encoding = Encoding.UTF8,
|
||||
Layout = "${message}"
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
var minLevel = runParameters.Verbose ? LogLevel.Trace : LogLevel.Info;
|
||||
config.LoggingRules.Add(
|
||||
new LoggingRule("*", minLevel, LogLevel.Fatal,
|
||||
new ColoredConsoleTarget
|
||||
|
@ -39,27 +52,12 @@ namespace Geekbot.Core.Logger
|
|||
Name = "Console",
|
||||
Encoding = Encoding.UTF8,
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue