Skip to content

Commit

Permalink
Moving to another place
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasAunvik committed May 17, 2020
1 parent 90d635e commit ff7b704
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 17 deletions.
197 changes: 197 additions & 0 deletions AnimeListBot/Handler/Logging/BotLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* This file is part of AnimeList Bot
*
* AnimeList Bot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AnimeList Bot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AnimeList Bot. If not, see <https://www.gnu.org/licenses/>
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using System.Globalization;
using Microsoft.Extensions.Logging;
using AnimeListBot.Handler.Logging;

namespace AnimeListBot.Handler
{
public class BotLogger : ILogger
{
const int MAX_FIELD_VALUE_LENGTH = 1024;

private readonly string _name;
private readonly BotLoggerConfiguration _config;

public BotLogger(string name, BotLoggerConfiguration config)
{
_name = name;
_config = config;
}

public void LogDiscordError(string errorMessage, IUser user = null, IGuildChannel guildChannel = null)
{
EmbedHandler embed = new EmbedHandler(user, "Error", string.Empty, true);
if (guildChannel != null)
{
embed.AddFieldSecure("Channel Info",
"Server Id: " + guildChannel.GuildId +
"\nChannel Id: <#" + guildChannel.Id + ">" +
"\nUser Id: <@" + user.Id + ">"
);
}
embed.AddFieldSecure("ErrorMessage", errorMessage);
}

public void LogDiscordError(LogMessage message)
{
EmbedHandler embed = new EmbedHandler(null, "Exception", string.Empty, true);
embed.AddFieldSecure("Severity", Enum.GetName(typeof(LogSeverity), message.Severity));
embed.AddFieldSecure("Source", message.Source);
embed.AddFieldSecure("Exception Message", message.Exception.Message);
if (message.Exception.StackTrace != null)
{
embed.AddFieldSecure("Type", message.Exception.GetType().FullName);
embed.AddFieldSecure("Stacktrace", TrancuateStacktrace(message.Exception.StackTrace));
}
}

public void LogDiscordError(Exception exception, IUser user = null, IGuildChannel guildChannel = null)
{
EmbedHandler embed = new EmbedHandler(user, "Exception", string.Empty, true);
if (guildChannel != null)
{
embed.AddFieldSecure("Channel Info",
"Server Id: " + guildChannel.GuildId +
"\nChannel Id: <#" + guildChannel.Id + ">" +
"\nUser Id: <@" + user.Id + ">"
);
}
embed.AddFieldSecure("Exception Message", exception.Message);
embed.AddFieldSecure("Type", exception.GetType().FullName);
embed.AddFieldSecure("Stacktrace", TrancuateStacktrace(exception.StackTrace));
}

public void LogDiscordError(CommandInfo info, ICommandContext context, IResult result)
{
if (result is ExecuteResult)
{
Exception e = ((ExecuteResult)result).Exception;

EmbedHandler embed = new EmbedHandler(context.User, "Command Exception", string.Empty, true);
embed.AddFieldSecure("Channel Info",
"Server Id: " + ((IGuildChannel)context.Channel).GuildId +
"\nChannel Id: <#" + context.Channel.Id + ">" +
"\nUser Id: <@" + context.User.Id + ">"
);
embed.AddFieldSecure("Command Used", context.Message.Content);
embed.AddFieldSecure("Exception Message", e.Message);
embed.AddFieldSecure("Type", e.GetType().FullName);
embed.AddFieldSecure("Stacktrace", TrancuateStacktrace(e.StackTrace));
}
}

public string TrancuateStacktrace(string input)
{
if (input == null) return "";

input = Format.Sanitize(input);
if (input.Length > MAX_FIELD_VALUE_LENGTH)
{
input = input.Substring(0, MAX_FIELD_VALUE_LENGTH);
}
return input;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
EmbedHandler embed = new EmbedHandler(null);
embed.Title = state.ToString();
formatter.Invoke(state, exception);


LogToFile(state.ToString());

if (_config.SendToOwner)
{
if (Program._client == null) return;

const ulong ownerId = 96580514021912576;
ulong channelId = Program.TestingMode ? Config.cached.test_error_channel : Config.cached.error_channel;
SocketUser owner = Program._client.GetUser(ownerId);

SocketTextChannel channel = (SocketTextChannel)Program._client.GetChannel(channelId);

if (owner != null)
{
owner?.GetOrCreateDMChannelAsync();
owner.SendMessageAsync("", false, embed.Build());
}

if (channel != null)
{
channel.SendMessageAsync("", false, embed.Build());
}
}
}

public void TryCreateLogFile()
{
if (!Directory.Exists(_config.FileDirectory))
{
Directory.CreateDirectory(_config.FileDirectory);
}
if (Directory.Exists(_config.FileDirectory))
{
using (FileStream logStream = File.Create(_config.FileDirectory + "/" + _config.FileName))
{
logStream.Close();
}
}
}

public void LogToFile(string message)
{
try
{
DateTime localTime = DateTime.Now;
string dateTimeMessage = "[" + localTime.ToString("T", DateTimeFormatInfo.InvariantInfo) + "] " + message;
string path = _config.FileDirectory + "/" + _config.FileName;
Console.WriteLine(dateTimeMessage);

using StreamWriter writer = new StreamWriter(path, true);
writer.WriteLine(dateTimeMessage);
}
catch (IOException e)
{
Console.WriteLine(e);
}
}

public bool IsEnabled(LogLevel logLevel)
{
return logLevel == _config.LogLevel;
}

public IDisposable BeginScope<TState>(TState state)
{
return null;
}
}
}
17 changes: 17 additions & 0 deletions AnimeListBot/Handler/Logging/BotLoggerConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace AnimeListBot.Handler.Logging
{
public class BotLoggerConfiguration
{
public LogLevel LogLevel { get; set; } = LogLevel.Warning;
public int EventId { get; set; } = 0;
public bool SendToOwner { get; set; } = false;

public string FileDirectory { get; set; } = "logs";
public string FileName { get; set; } = "newest.log";
}
}
32 changes: 32 additions & 0 deletions AnimeListBot/Handler/Logging/BotLoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace AnimeListBot.Handler.Logging
{
public static class BotLoggerExtensions
{
public static ILoggerFactory AddBotLogger(
this ILoggerFactory loggerFactory,
BotLoggerConfiguration config)
{
loggerFactory.AddProvider(new BotLoggerProvider(config));
return loggerFactory;
}
public static ILoggerFactory AddBotLogger(
this ILoggerFactory loggerFactory)
{
var config = new BotLoggerConfiguration();
return loggerFactory.AddBotLogger(config);
}
public static ILoggerFactory AddBotLogger(
this ILoggerFactory loggerFactory,
Action<BotLoggerConfiguration> configure)
{
var config = new BotLoggerConfiguration();
configure(config);
return loggerFactory.AddBotLogger(config);
}
}
}
29 changes: 29 additions & 0 deletions AnimeListBot/Handler/Logging/BotLoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace AnimeListBot.Handler.Logging
{
public class BotLoggerProvider : ILoggerProvider
{
private readonly BotLoggerConfiguration _config;
private readonly ConcurrentDictionary<string, BotLogger> _loggers = new ConcurrentDictionary<string, BotLogger>();

public BotLoggerProvider(BotLoggerConfiguration config)
{
_config = config;
}

public ILogger CreateLogger(string categoryName)
{
return _loggers.GetOrAdd(categoryName, name => new BotLogger(name, _config));
}

public void Dispose()
{
_loggers.Clear();
}
}
}
2 changes: 2 additions & 0 deletions AnimeListBot/Handler/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
using Discord.Commands;
using Discord.WebSocket;
using System.Globalization;
using Microsoft.Extensions.Logging;
using AnimeListBot.Handler.Logging;

namespace AnimeListBot.Handler
{
Expand Down
7 changes: 6 additions & 1 deletion AnimeListBot/Modules/Administrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@
using Microsoft.EntityFrameworkCore.Storage;
using JikanDotNet;
using AnimeListBot.Handler.Database;
using Microsoft.Extensions.Logging;

namespace AnimeListBot.Modules
{
[RequireOwner]
public class Administrator : ModuleBase<ShardedCommandContext>
{
private IDatabaseService _db;
private ILogger<BotLogger> _logger;

public Administrator(IDatabaseService db)
public Administrator(IDatabaseService db, ILogger<BotLogger> logger)
{
_db = db;
_logger = logger;
}

[Command("stop")]
Expand Down Expand Up @@ -223,6 +226,8 @@ public async Task TestCommand()
DiscordServer server = await _db.GetServerById(Context.Guild.Id);
embed.Description = server.Prefix;

_logger.LogError("Test");

await _db.SaveChangesAsync();
await embed.SendMessage(Context.Channel);
}
Expand Down
Loading

0 comments on commit ff7b704

Please sign in to comment.