Use LogSource Enum for logger and improve logging messages
This commit is contained in:
parent
0fe273151c
commit
2b85caeb29
16 changed files with 122 additions and 95 deletions
|
@ -42,10 +42,10 @@ namespace Geekbot.net.Lib.Clients
|
|||
}
|
||||
_animeSearch = new AnimeSearchMethodsAsync(_credentials);
|
||||
_mangaSearch = new MangaSearchMethodsAsync(_credentials);
|
||||
_logger.Debug("Geekbot", "Logged in to MAL");
|
||||
_logger.Debug(LogSource.Geekbot, "Logged in to MAL");
|
||||
return true;
|
||||
}
|
||||
_logger.Debug("Geekbot", "No MAL Credentials Set!");
|
||||
_logger.Debug(LogSource.Geekbot, "No MAL Credentials Set!");
|
||||
return false;
|
||||
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ namespace Geekbot.net.Lib.ErrorHandling
|
|||
if (!string.IsNullOrEmpty(sentryDsn))
|
||||
{
|
||||
_raven = new RavenClient(sentryDsn);
|
||||
_logger.Information("Geekbot", $"Command Errors will be logged to Sentry: {sentryDsn}");
|
||||
_logger.Information(LogSource.Geekbot, $"Command Errors will be logged to Sentry: {sentryDsn}");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -42,14 +42,21 @@ namespace Geekbot.net.Lib.ErrorHandling
|
|||
var errorObj = SimpleConextConverter.ConvertContext(context);
|
||||
if (e.Message.Contains("50007")) return;
|
||||
if (e.Message.Contains("50013")) return;
|
||||
_logger.Error("Geekbot", "An error ocured", e, errorObj);
|
||||
_logger.Error(LogSource.Geekbot, "An error ocured", e, errorObj);
|
||||
if (!string.IsNullOrEmpty(errorMessage))
|
||||
{
|
||||
if (_errorsInChat)
|
||||
{
|
||||
var resStackTrace = string.IsNullOrEmpty(e.InnerException?.ToString()) ? e.StackTrace : e.InnerException.ToString();
|
||||
var maxLen = Math.Min(resStackTrace.Length, 1850);
|
||||
context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{resStackTrace.Substring(0, maxLen)}\r\n```");
|
||||
var resStackTrace = string.IsNullOrEmpty(e.InnerException?.ToString()) ? e.StackTrace : e.InnerException?.ToString();
|
||||
if (!string.IsNullOrEmpty(resStackTrace))
|
||||
{
|
||||
var maxLen = Math.Min(resStackTrace.Length, 1850);
|
||||
context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{resStackTrace.Substring(0, maxLen)}\r\n```");
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Channel.SendMessageAsync(e.Message);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -75,7 +82,7 @@ namespace Geekbot.net.Lib.ErrorHandling
|
|||
catch (Exception ex)
|
||||
{
|
||||
context.Channel.SendMessageAsync("Something went really really wrong here");
|
||||
_logger.Error("Geekbot", "Errorception", ex);
|
||||
_logger.Error(LogSource.Geekbot, "Errorception", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ namespace Geekbot.net.Lib.Localization
|
|||
{
|
||||
_logger = logger;
|
||||
_redis = redis;
|
||||
_logger.Information("Geekbot", "Loading Translations");
|
||||
_logger.Information(LogSource.Geekbot, "Loading Translations");
|
||||
LoadTranslations();
|
||||
LoadServerLanguages(clientGuilds);
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ namespace Geekbot.net.Lib.Localization
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error("Geekbot", "Failed to load Translations", e);
|
||||
_logger.Error(LogSource.Geekbot, "Failed to load Translations", e);
|
||||
Environment.Exit(110);
|
||||
}
|
||||
}
|
||||
|
@ -100,7 +100,7 @@ namespace Geekbot.net.Lib.Localization
|
|||
translation = _translations[command][stringName]["EN"];
|
||||
if (string.IsNullOrWhiteSpace(translation))
|
||||
{
|
||||
_logger.Warning("Geekbot", $"No translation found for {command} - {stringName}");
|
||||
_logger.Warning(LogSource.Geekbot, $"No translation found for {command} - {stringName}");
|
||||
}
|
||||
return translation;
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ namespace Geekbot.net.Lib.Localization
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error("Geekbot", "lol nope", e);
|
||||
_logger.Error(LogSource.Geekbot, "lol nope", e);
|
||||
return new Dictionary<string, string>();
|
||||
}
|
||||
}
|
||||
|
@ -127,7 +127,7 @@ namespace Geekbot.net.Lib.Localization
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error("Geekbot", "lol nope", e);
|
||||
_logger.Error(LogSource.Geekbot, "lol nope", e);
|
||||
return new Dictionary<string, string>();
|
||||
}
|
||||
}
|
||||
|
@ -143,7 +143,7 @@ namespace Geekbot.net.Lib.Localization
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error("Geekbot", "Error while changing language", e);
|
||||
_logger.Error(LogSource.Geekbot, "Error while changing language", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Geekbot.net.Commands.Randomness.Cat;
|
||||
|
||||
namespace Geekbot.net.Lib.Logger
|
||||
{
|
||||
|
@ -14,26 +16,37 @@ namespace Geekbot.net.Lib.Logger
|
|||
|
||||
public Task Log(LogMessage message)
|
||||
{
|
||||
LogSource source;
|
||||
try
|
||||
{
|
||||
source = Enum.Parse<LogSource>(message.Source);
|
||||
}
|
||||
catch
|
||||
{
|
||||
source = LogSource.Discord;
|
||||
_logger.Warning(LogSource.Geekbot, $"Could not parse {message.Source} to a LogSource Enum");
|
||||
}
|
||||
|
||||
var logMessage = $"[{message.Source}] {message.Message}";
|
||||
switch (message.Severity)
|
||||
{
|
||||
case LogSeverity.Verbose:
|
||||
_logger.Trace(message.Source, message.Message);
|
||||
_logger.Trace(source, message.Message);
|
||||
break;
|
||||
case LogSeverity.Debug:
|
||||
_logger.Debug(message.Source, message.Message);
|
||||
_logger.Debug(source, message.Message);
|
||||
break;
|
||||
case LogSeverity.Info:
|
||||
_logger.Information(message.Source, message.Message);
|
||||
_logger.Information(source, message.Message);
|
||||
break;
|
||||
case LogSeverity.Critical:
|
||||
case LogSeverity.Error:
|
||||
case LogSeverity.Warning:
|
||||
if (logMessage.Contains("VOICE_STATE_UPDATE")) break;
|
||||
_logger.Error(message.Source, message.Message, message.Exception);
|
||||
_logger.Error(source, message.Message, message.Exception);
|
||||
break;
|
||||
default:
|
||||
_logger.Information(message.Source, $"{logMessage} --- {message.Severity}");
|
||||
_logger.Information(source, $"{logMessage} --- {message.Severity}");
|
||||
break;
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
|
|
|
@ -18,35 +18,36 @@ namespace Geekbot.net.Lib.Logger
|
|||
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
|
||||
NullValueHandling = NullValueHandling.Include
|
||||
};
|
||||
Information("Geekbot", "Using GeekbotLogger");
|
||||
Information(LogSource.Geekbot, "Using GeekbotLogger");
|
||||
}
|
||||
|
||||
public void Trace(string source, string message, object extra = null)
|
||||
public void Trace(LogSource source, string message, object extra = null)
|
||||
{
|
||||
_logger.Trace(CreateLogString("Trace", source, message, null, extra));
|
||||
}
|
||||
|
||||
public void Debug(string source, string message, object extra = null)
|
||||
public void Debug(LogSource source, string message, object extra = null)
|
||||
{
|
||||
_logger.Debug(CreateLogString("Debug", source, message, null, extra));
|
||||
}
|
||||
|
||||
public void Information(string source, string message, object extra = null)
|
||||
public void Information(LogSource source, string message, object extra = null)
|
||||
{
|
||||
_logger.Info(CreateLogString("Information", source, message, null, extra));
|
||||
}
|
||||
|
||||
public void Warning(string 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));
|
||||
}
|
||||
|
||||
public void Error(string source, string message, Exception stackTrace, object extra = null)
|
||||
public void Error(LogSource source, string message, Exception stackTrace, object extra = null)
|
||||
{
|
||||
_logger.Error(CreateLogString("Error", source, message, stackTrace, extra));
|
||||
if (_logAsJson) _logger.Error(CreateLogString("Error", source, message, stackTrace, extra));
|
||||
else _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra));
|
||||
}
|
||||
|
||||
private string CreateLogString(string type, string source, string message, Exception stackTrace = null, object extra = null)
|
||||
private string CreateLogString(string type, LogSource source, string message, Exception stackTrace = null, object extra = null)
|
||||
{
|
||||
if (_logAsJson)
|
||||
{
|
||||
|
@ -62,7 +63,7 @@ namespace Geekbot.net.Lib.Logger
|
|||
return JsonConvert.SerializeObject(logObject, Formatting.None, _serializerSettings);
|
||||
}
|
||||
|
||||
if (source != "Message") return $"[{source}] - {message}";
|
||||
if (source != LogSource.Message) return $"[{source}] - {message}";
|
||||
|
||||
var m = (MessageDto) extra;
|
||||
return $"[{source}] - [{m?.Guild.Name} - {m?.Channel.Name}] {m?.User.Name}: {m?.Message.Content}";
|
||||
|
|
|
@ -4,10 +4,10 @@ namespace Geekbot.net.Lib.Logger
|
|||
{
|
||||
public interface IGeekbotLogger
|
||||
{
|
||||
void Trace(string source, string message, object extra = null);
|
||||
void Debug(string source, string message, object extra = null);
|
||||
void Information(string source, string message, object extra = null);
|
||||
void Warning(string source, string message, Exception stackTrace = null, object extra = null);
|
||||
void Error(string source, string message, Exception stackTrace, object extra = null);
|
||||
void Trace(LogSource source, string message, object extra = null);
|
||||
void Debug(LogSource source, string message, object extra = null);
|
||||
void Information(LogSource source, string message, object extra = null);
|
||||
void Warning(LogSource source, string message, Exception stackTrace = null, object extra = null);
|
||||
void Error(LogSource source, string message, Exception stackTrace, object extra = null);
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ namespace Geekbot.net.Lib.Logger
|
|||
{
|
||||
public DateTime Timestamp { get; set; }
|
||||
public string Type { get; set; }
|
||||
public string Source { get; set; }
|
||||
public LogSource Source { get; set; }
|
||||
public string Message { get; set; }
|
||||
public Exception StackTrace { get; set; }
|
||||
public object Extra { get; set; }
|
||||
|
|
16
Geekbot.net/Lib/Logger/LogSource.cs
Normal file
16
Geekbot.net/Lib/Logger/LogSource.cs
Normal file
|
@ -0,0 +1,16 @@
|
|||
namespace Geekbot.net.Lib.Logger
|
||||
{
|
||||
public enum LogSource
|
||||
{
|
||||
Geekbot,
|
||||
Rest,
|
||||
Gateway,
|
||||
Discord,
|
||||
Redis,
|
||||
Message,
|
||||
UserRepository,
|
||||
Command,
|
||||
Api,
|
||||
Other
|
||||
}
|
||||
}
|
|
@ -56,13 +56,13 @@ namespace Geekbot.net.Lib.Logger
|
|||
},
|
||||
Guild = new MessageDto.IdAndName
|
||||
{
|
||||
Id = channel.Guild.Id.ToString(),
|
||||
Name = channel.Guild.Name
|
||||
Id = channel?.Guild?.Id.ToString(),
|
||||
Name = channel?.Guild?.Name
|
||||
},
|
||||
Channel = new MessageDto.IdAndName
|
||||
{
|
||||
Id = channel.Id.ToString(),
|
||||
Name = channel.Name
|
||||
Id = channel?.Id.ToString(),
|
||||
Name = channel?.Name
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -17,11 +17,11 @@ namespace Geekbot.net.Lib.Media
|
|||
var rawFortunes = File.ReadAllText(path);
|
||||
_fortuneArray = rawFortunes.Split("%");
|
||||
_totalFortunes = _fortuneArray.Length;
|
||||
logger.Trace("Geekbot", $"Loaded {_totalFortunes} Fortunes");
|
||||
logger.Trace(LogSource.Geekbot, $"Loaded {_totalFortunes} Fortunes");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Information("Geekbot", $"Fortunes File not found at {path}");
|
||||
logger.Information(LogSource.Geekbot, $"Fortunes File not found at {path}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ namespace Geekbot.net.Lib.Media
|
|||
_random = new Random();
|
||||
_logger = logger;
|
||||
|
||||
logger.Information("Geekbot", "Loading Media Files");
|
||||
logger.Information(LogSource.Geekbot, "Loading Media Files");
|
||||
|
||||
LoadCheckem();
|
||||
LoadPandas();
|
||||
|
@ -38,56 +38,56 @@ namespace Geekbot.net.Lib.Media
|
|||
{
|
||||
var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/checkEmPics"));
|
||||
_checkemImages = rawLinks.Split("\n");
|
||||
_logger.Trace("Geekbot", $"Loaded {_checkemImages.Length} CheckEm Images");
|
||||
_logger.Trace(LogSource.Geekbot, $"Loaded {_checkemImages.Length} CheckEm Images");
|
||||
}
|
||||
|
||||
private void LoadPandas()
|
||||
{
|
||||
var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/pandas"));
|
||||
_pandaImages = rawLinks.Split("\n");
|
||||
_logger.Trace("Geekbot", $"Loaded {_pandaImages.Length} Panda Images");
|
||||
_logger.Trace(LogSource.Geekbot, $"Loaded {_pandaImages.Length} Panda Images");
|
||||
}
|
||||
|
||||
private void BakeCroissants()
|
||||
{
|
||||
var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/croissant"));
|
||||
_croissantImages = rawLinks.Split("\n");
|
||||
_logger.Trace("Geekbot", $"Loaded {_croissantImages.Length} Croissant Images");
|
||||
_logger.Trace(LogSource.Geekbot, $"Loaded {_croissantImages.Length} Croissant Images");
|
||||
}
|
||||
|
||||
private void LoadSquirrels()
|
||||
{
|
||||
var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/squirrel"));
|
||||
_squirrelImages = rawLinks.Split("\n");
|
||||
_logger.Trace("Geekbot", $"Loaded {_squirrelImages.Length} Squirrel Images");
|
||||
_logger.Trace(LogSource.Geekbot, $"Loaded {_squirrelImages.Length} Squirrel Images");
|
||||
}
|
||||
|
||||
private void LoadPumpkins()
|
||||
{
|
||||
var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/pumpkin"));
|
||||
_pumpkinImages = rawLinks.Split("\n");
|
||||
_logger.Trace("Geekbot", $"Loaded {_pumpkinImages.Length} Pumpkin Images");
|
||||
_logger.Trace(LogSource.Geekbot, $"Loaded {_pumpkinImages.Length} Pumpkin Images");
|
||||
}
|
||||
|
||||
private void LoadTurtles()
|
||||
{
|
||||
var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/turtles"));
|
||||
_turtlesImages = rawLinks.Split("\n");
|
||||
_logger.Trace("Geekbot", $"Loaded {_turtlesImages.Length} Turtle Images");
|
||||
_logger.Trace(LogSource.Geekbot, $"Loaded {_turtlesImages.Length} Turtle Images");
|
||||
}
|
||||
|
||||
private void LoadPinguins()
|
||||
{
|
||||
var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/pinguins"));
|
||||
_pinguinImages = rawLinks.Split("\n");
|
||||
_logger.Trace("Geekbot", $"Loaded {_pinguinImages.Length} Pinguin Images");
|
||||
_logger.Trace(LogSource.Geekbot, $"Loaded {_pinguinImages.Length} Pinguin Images");
|
||||
}
|
||||
|
||||
private void LoadFoxes()
|
||||
{
|
||||
var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/foxes"));
|
||||
_foxImages = rawLinks.Split("\n");
|
||||
_logger.Trace("Geekbot", $"Loaded {_foxImages.Length} Foxes Images");
|
||||
_logger.Trace(LogSource.Geekbot, $"Loaded {_foxImages.Length} Foxes Images");
|
||||
}
|
||||
|
||||
public string GetCheckem()
|
||||
|
|
|
@ -36,12 +36,12 @@ namespace Geekbot.net.Lib.UserRepository
|
|||
}
|
||||
Store(savedUser);
|
||||
|
||||
_logger.Information("UserRepository", "Updated User", savedUser);
|
||||
_logger.Information(LogSource.UserRepository, "Updated User", savedUser);
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Warning("UserRepository", $"Failed to update user: {user.Username}#{user.Discriminator} ({user.Id})", e);
|
||||
_logger.Warning(LogSource.UserRepository, $"Failed to update user: {user.Username}#{user.Discriminator} ({user.Id})", e);
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ namespace Geekbot.net.Lib.UserRepository
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Warning("UserRepository", "Failed to get {userId} from repository", e);
|
||||
_logger.Warning(LogSource.UserRepository, $"Failed to get {userId} from repository", e);
|
||||
return new UserRepositoryUser();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue