CreateUserLoggingConfigFile.cs 7.2 KB

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