-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLogger.cs
More file actions
57 lines (51 loc) · 1.8 KB
/
FileLogger.cs
File metadata and controls
57 lines (51 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.IO;
namespace Jellyfin.Plugin.AniFin;
public static class FileLogger
{
private static string? _logPath;
private static readonly object _lock = new object();
public static void Initialize()
{
try
{
var pluginPath = Plugin.Instance?.GetType().Assembly.Location;
if (!string.IsNullOrEmpty(pluginPath))
{
var pluginDir = Path.GetDirectoryName(pluginPath);
if (!string.IsNullOrEmpty(pluginDir))
{
var logsDir = Path.Combine(pluginDir, "logs");
Directory.CreateDirectory(logsDir);
_logPath = Path.Combine(logsDir, $"anifin-{DateTime.Now:yyyy-MM-dd}.log");
Log("INFO", "FileLogger initialized");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to initialize FileLogger: {ex.Message}");
}
}
public static void Log(string level, string message)
{
if (string.IsNullOrEmpty(_logPath)) Initialize();
if (string.IsNullOrEmpty(_logPath)) return;
try
{
lock (_lock)
{
var logEntry = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{level}] {message}{Environment.NewLine}";
File.AppendAllText(_logPath, logEntry);
}
}
catch
{
// Ignore logging errors
}
}
public static void Info(string message) => Log("INFO", message);
public static void Error(string message) => Log("ERROR", message);
public static void Debug(string message) => Log("DEBUG", message);
public static void Warning(string message) => Log("WARN", message);
}