CreateUserLoggingConfigFile.cs 7.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using MediaBrowser.Common.Configuration;
  6. using Microsoft.Extensions.Logging;
  7. using Newtonsoft.Json.Linq;
  8. namespace Jellyfin.Server.Migrations.Routines
  9. {
  10. /// <summary>
  11. /// Migration to initialize the user logging configuration file "logging.user.json".
  12. /// If the deprecated logging.json file exists and has a custom config, it will be used as logging.user.json,
  13. /// otherwise a blank file will be created.
  14. /// </summary>
  15. internal class CreateUserLoggingConfigFile : IMigrationRoutine
  16. {
  17. /// <summary>
  18. /// File history for logging.json as existed during this migration creation. The contents for each has been minified.
  19. /// </summary>
  20. private readonly List<string> _defaultConfigHistory = new List<string>
  21. {
  22. // 9a6c27947353585391e211aa88b925f81e8cd7b9
  23. @"{""Serilog"":{""MinimumLevel"":{""Default"":""Information"",""Override"":{""Microsoft"":""Warning"",""System"":""Warning""}},""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTemplate"":""[{Timestamp:HH:mm:ss}] [{Level:u3}] [{ThreadId}] {SourceContext}: {Message:lj}{NewLine}{Exception}""}},{""Name"":""Async"",""Args"":{""configure"":[{""Name"":""File"",""Args"":{""path"":""%JELLYFIN_LOG_DIR%//log_.log"",""rollingInterval"":""Day"",""retainedFileCountLimit"":3,""rollOnFileSizeLimit"":true,""fileSizeLimitBytes"":100000000,""outputTemplate"":""[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] [{ThreadId}] {SourceContext}: {Message}{NewLine}{Exception}""}}]}}],""Enrich"":[""FromLogContext"",""WithThreadId""]}}",
  24. // 71bdcd730705a714ee208eaad7290b7c68df3885
  25. @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTemplate"":""[{Timestamp:HH:mm:ss}] [{Level:u3}] [{ThreadId}] {SourceContext}: {Message:lj}{NewLine}{Exception}""}},{""Name"":""Async"",""Args"":{""configure"":[{""Name"":""File"",""Args"":{""path"":""%JELLYFIN_LOG_DIR%//log_.log"",""rollingInterval"":""Day"",""retainedFileCountLimit"":3,""rollOnFileSizeLimit"":true,""fileSizeLimitBytes"":100000000,""outputTemplate"":""[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] [{ThreadId}] {SourceContext}: {Message}{NewLine}{Exception}""}}]}}],""Enrich"":[""FromLogContext"",""WithThreadId""]}}",
  26. // a44936f97f8afc2817d3491615a7cfe1e31c251c
  27. @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTemplate"":""[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message:lj}{NewLine}{Exception}""}},{""Name"":""File"",""Args"":{""path"":""%JELLYFIN_LOG_DIR%//log_.log"",""rollingInterval"":""Day"",""outputTemplate"":""[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] {Message}{NewLine}{Exception}""}}]}}",
  28. // 7af3754a11ad5a4284f107997fb5419a010ce6f3
  29. @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTemplate"":""[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message:lj}{NewLine}{Exception}""}},{""Name"":""Async"",""Args"":{""configure"":[{""Name"":""File"",""Args"":{""path"":""%JELLYFIN_LOG_DIR%//log_.log"",""rollingInterval"":""Day"",""outputTemplate"":""[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] {Message}{NewLine}{Exception}""}}]}}]}}",
  30. // 60691349a11f541958e0b2247c9abc13cb40c9fb
  31. @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTemplate"":""[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message:lj}{NewLine}{Exception}""}},{""Name"":""Async"",""Args"":{""configure"":[{""Name"":""File"",""Args"":{""path"":""%JELLYFIN_LOG_DIR%//log_.log"",""rollingInterval"":""Day"",""retainedFileCountLimit"":3,""rollOnFileSizeLimit"":true,""fileSizeLimitBytes"":100000000,""outputTemplate"":""[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] {Message}{NewLine}{Exception}""}}]}}]}}",
  32. // 65fe243afbcc4b596cf8726708c1965cd34b5f68
  33. @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTemplate"":""[{Timestamp:HH:mm:ss}] [{Level:u3}] {ThreadId} {SourceContext}: {Message:lj} {NewLine}{Exception}""}},{""Name"":""Async"",""Args"":{""configure"":[{""Name"":""File"",""Args"":{""path"":""%JELLYFIN_LOG_DIR%//log_.log"",""rollingInterval"":""Day"",""retainedFileCountLimit"":3,""rollOnFileSizeLimit"":true,""fileSizeLimitBytes"":100000000,""outputTemplate"":""[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] {ThreadId} {SourceContext}:{Message} {NewLine}{Exception}""}}]}}],""Enrich"":[""FromLogContext"",""WithThreadId""]}}",
  34. // 96c9af590494aa8137d5a061aaf1e68feee60b67
  35. @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTemplate"":""[{Timestamp:HH:mm:ss}] [{Level:u3}] [{ThreadId}] {SourceContext}: {Message:lj}{NewLine}{Exception}""}},{""Name"":""Async"",""Args"":{""configure"":[{""Name"":""File"",""Args"":{""path"":""%JELLYFIN_LOG_DIR%//log_.log"",""rollingInterval"":""Day"",""retainedFileCountLimit"":3,""rollOnFileSizeLimit"":true,""fileSizeLimitBytes"":100000000,""outputTemplate"":""[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] [{ThreadId}] {SourceContext}:{Message}{NewLine}{Exception}""}}]}}],""Enrich"":[""FromLogContext"",""WithThreadId""]}}",
  36. };
  37. private readonly IApplicationPaths _appPaths;
  38. public CreateUserLoggingConfigFile(IApplicationPaths appPaths)
  39. {
  40. _appPaths = appPaths;
  41. }
  42. /// <inheritdoc/>
  43. public Guid Id => Guid.Parse("{EF103419-8451-40D8-9F34-D1A8E93A1679}");
  44. /// <inheritdoc/>
  45. public string Name => "CreateLoggingConfigHeirarchy";
  46. /// <inheritdoc/>
  47. public void Perform()
  48. {
  49. var logDirectory = _appPaths.ConfigurationDirectoryPath;
  50. var existingConfigPath = Path.Combine(logDirectory, "logging.json");
  51. // If the existing logging.json config file is unmodified, then 'reset' it by moving it to 'logging.old.json'
  52. // NOTE: This config file has 'reloadOnChange: true', so this change will take effect immediately even though it has already been loaded
  53. if (File.Exists(existingConfigPath) && ExistingConfigUnmodified(existingConfigPath))
  54. {
  55. File.Move(existingConfigPath, Path.Combine(logDirectory, "logging.old.json"));
  56. }
  57. }
  58. /// <summary>
  59. /// Check if the existing logging.json file has not been modified by the user by comparing it to all the
  60. /// versions in our git history. Until now, the file has never been migrated after first creation so users
  61. /// could have any version from the git history.
  62. /// </summary>
  63. /// <exception cref="IOException"><paramref name="oldConfigPath"/> does not exist or could not be read.</exception>
  64. private bool ExistingConfigUnmodified(string oldConfigPath)
  65. {
  66. var existingConfigJson = JToken.Parse(File.ReadAllText(oldConfigPath));
  67. return _defaultConfigHistory
  68. .Select(historicalConfigText => JToken.Parse(historicalConfigText))
  69. .Any(historicalConfigJson => JToken.DeepEquals(existingConfigJson, historicalConfigJson));
  70. }
  71. }
  72. }