diff --git a/src/Recyclarr.Tests/LogJanitorTest.cs b/src/Recyclarr.Tests/LogJanitorTest.cs index 9c2fd5e8..dd20fafb 100644 --- a/src/Recyclarr.Tests/LogJanitorTest.cs +++ b/src/Recyclarr.Tests/LogJanitorTest.cs @@ -1,6 +1,7 @@ using System.IO.Abstractions; using NSubstitute; using NUnit.Framework; +using Recyclarr.Logging; namespace Recyclarr.Tests; diff --git a/src/Recyclarr/Command/Initialization/OldLogFileCleaner.cs b/src/Recyclarr/Command/Initialization/OldLogFileCleaner.cs index 954913ac..950e42b9 100644 --- a/src/Recyclarr/Command/Initialization/OldLogFileCleaner.cs +++ b/src/Recyclarr/Command/Initialization/OldLogFileCleaner.cs @@ -1,4 +1,6 @@ -namespace Recyclarr.Command.Initialization; +using Recyclarr.Logging; + +namespace Recyclarr.Command.Initialization; internal class OldLogFileCleaner : IServiceCleaner { diff --git a/src/Recyclarr/CompositionRoot.cs b/src/Recyclarr/CompositionRoot.cs index 8069477e..fbdd8c58 100644 --- a/src/Recyclarr/CompositionRoot.cs +++ b/src/Recyclarr/CompositionRoot.cs @@ -10,6 +10,7 @@ using Recyclarr.Command.Helpers; using Recyclarr.Command.Initialization; using Recyclarr.Command.Services; using Recyclarr.Config; +using Recyclarr.Logging; using Recyclarr.Migration; using Serilog; using Serilog.Core; @@ -31,19 +32,8 @@ public static class CompositionRoot { builder.RegisterType().As(); builder.RegisterType().SingleInstance(); - builder.Register(c => - { - var logPath = Path.Combine(AppPaths.LogDirectory, - $"trash_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.log"); - - const string consoleTemplate = "[{Level:u3}] {Message:lj}{NewLine}{Exception}"; - - return new LoggerConfiguration() - .MinimumLevel.Debug() - .WriteTo.Console(outputTemplate: consoleTemplate, levelSwitch: c.Resolve()) - .WriteTo.File(logPath) - .CreateLogger(); - }) + builder.RegisterType(); + builder.Register(c => c.Resolve().Create()) .As() .SingleInstance(); } diff --git a/src/Recyclarr/ILogJanitor.cs b/src/Recyclarr/Logging/ILogJanitor.cs similarity index 75% rename from src/Recyclarr/ILogJanitor.cs rename to src/Recyclarr/Logging/ILogJanitor.cs index 08128c6b..687163fc 100644 --- a/src/Recyclarr/ILogJanitor.cs +++ b/src/Recyclarr/Logging/ILogJanitor.cs @@ -1,4 +1,4 @@ -namespace Recyclarr; +namespace Recyclarr.Logging; public interface ILogJanitor { diff --git a/src/Recyclarr/LogJanitor.cs b/src/Recyclarr/Logging/LogJanitor.cs similarity index 94% rename from src/Recyclarr/LogJanitor.cs rename to src/Recyclarr/Logging/LogJanitor.cs index d4223721..a50dbe9e 100644 --- a/src/Recyclarr/LogJanitor.cs +++ b/src/Recyclarr/Logging/LogJanitor.cs @@ -1,6 +1,6 @@ using System.IO.Abstractions; -namespace Recyclarr; +namespace Recyclarr.Logging; public class LogJanitor : ILogJanitor { diff --git a/src/Recyclarr/Logging/LoggerFactory.cs b/src/Recyclarr/Logging/LoggerFactory.cs new file mode 100644 index 00000000..84a4a4e8 --- /dev/null +++ b/src/Recyclarr/Logging/LoggerFactory.cs @@ -0,0 +1,30 @@ +using System.IO.Abstractions; +using Serilog; +using Serilog.Core; + +namespace Recyclarr.Logging; + +public class LoggerFactory +{ + private readonly IFileSystem _fs; + private readonly LoggingLevelSwitch _logLevel; + + public LoggerFactory(IFileSystem fs, LoggingLevelSwitch logLevel) + { + _fs = fs; + _logLevel = logLevel; + } + + public ILogger Create() + { + var logPath = _fs.Path.Combine(AppPaths.LogDirectory, $"trash_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.log"); + + const string consoleTemplate = "[{Level:u3}] {Message:lj}{NewLine}{Exception}"; + + return new LoggerConfiguration() + .MinimumLevel.Debug() + .WriteTo.Console(outputTemplate: consoleTemplate, levelSwitch: _logLevel) + .WriteTo.File(logPath) + .CreateLogger(); + } +}