From 54685289afb8db6158a3a5bb1fda318d04092545 Mon Sep 17 00:00:00 2001 From: walon Date: Thu, 29 Jul 2021 15:48:15 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91Luban.Clien?= =?UTF-8?q?t=E5=92=8CLuban.Server=20=E6=96=B0=E5=A2=9E=20-l=20(--loglevel)?= =?UTF-8?q?=20=E5=8F=82=E6=95=B0=EF=BC=8C=E5=85=81=E8=AE=B8=E6=8C=89?= =?UTF-8?q?=E7=90=86=E6=97=A5=E5=BF=97=E7=BA=A7=E5=88=AB=E3=80=82=E5=8F=A6?= =?UTF-8?q?=E5=A4=96=EF=BC=8C=E5=AF=B9=E4=BA=8E=20<=3D=20DEBUG=E7=9A=84?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=BA=A7=E5=88=AB=EF=BC=8C=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=BB=A3=E7=A0=81=E4=BD=8D=E7=BD=AE=20?= =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91=E7=89=B9=E6=AE=8A=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20Luban.Client=20=E7=94=B1=E4=BA=8E=E6=9C=AA=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=20LUBAN=5FSERVER=5FIP=20=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E5=AF=BC=E8=87=B4-h=20=E5=8F=82=E6=95=B0=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=E8=80=8C=E8=BF=90=E8=A1=8C=E5=A4=B1=E8=B4=A5=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E6=AD=A4=E6=97=B6=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=8F=96=20127.0.0.1=20=E5=B9=B6=E6=89=93=E5=8D=B0=E8=AD=A6?= =?UTF-8?q?=E5=91=8A=E6=97=A5=E5=BF=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Luban.Client/Source/Program.cs | 23 ++++++++++++++++++++--- src/Luban.Common/Source/Utils/LogUtil.cs | 11 +++++++++-- src/Luban.Server/Source/Program.cs | 5 ++++- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/Luban.Client/Source/Program.cs b/src/Luban.Client/Source/Program.cs index ba9d68d..d52b04c 100644 --- a/src/Luban.Client/Source/Program.cs +++ b/src/Luban.Client/Source/Program.cs @@ -26,6 +26,8 @@ namespace Luban.Client public bool Verbose { get; set; } + public string LogLevel { get; set; } = "INFO"; + public string CacheMetaInfoFile { get; set; } = ".cache.meta"; public string[] WatchDir { get; set; } @@ -47,6 +49,7 @@ Options: -p --port port. default 8899 -j --job Required. job type. avaliable value: cfg -v --verbose verbose print + -l --loglevel log level. default INFO. avaliable value: TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF -c --cachemetafile cache meta file name. default is '.cache.meta' -w --watch watch data change and regenerate. -h --help show usage @@ -67,8 +70,16 @@ Options: case "-h": case "--host": { - - ops.Host = args[++i]; + // 打个补丁。好多人忘了设置 LUBAN_SERVER_IP 环境变量,导致启动时出问题 + if (args[i + 1].StartsWith("-")) + { + Console.WriteLine("[WARN] --host (or -h) argument is missing, use 127.0.0.1 as default. do you forget to set LUBAN_SERVER_IP env variable?"); + ops.Host = "127.0.0.1"; + } + else + { + ops.Host = args[++i]; + } break; } case "-p": @@ -89,6 +100,12 @@ Options: ops.Verbose = true; break; } + case "-l": + case "--loglevel": + { + ops.LogLevel = args[++i]; + break; + } case "-c": case "--cachemetafile": { @@ -148,7 +165,7 @@ Options: CommandLineOptions options = parseResult.Item2; profile.StartPhase("init logger"); - LogUtil.InitSimpleNLogConfigure(NLog.LogLevel.Info); + Luban.Common.Utils.LogUtil.InitSimpleNLogConfigure(NLog.LogLevel.FromString(options.LogLevel)); s_logger = NLog.LogManager.GetCurrentClassLogger(); profile.EndPhaseAndLog(); diff --git a/src/Luban.Common/Source/Utils/LogUtil.cs b/src/Luban.Common/Source/Utils/LogUtil.cs index 3c99316..13420b1 100644 --- a/src/Luban.Common/Source/Utils/LogUtil.cs +++ b/src/Luban.Common/Source/Utils/LogUtil.cs @@ -5,8 +5,15 @@ namespace Luban.Common.Utils public static void InitSimpleNLogConfigure(NLog.LogLevel minConsoleLogLevel) { var logConfig = new NLog.Config.LoggingConfiguration(); - //var layout = NLog.Layouts.Layout.FromString("${longdate}|${level:uppercase=true}|${threadid}|${message}${onexception:${newline}${exception:format=tostring}${exception:format=StackTrace}}"); - var layout = NLog.Layouts.Layout.FromString("${longdate}|${message}${onexception:${newline}${exception:format=tostring}${exception:format=StackTrace}}"); + NLog.Layouts.Layout layout; + if (minConsoleLogLevel <= NLog.LogLevel.Debug) + { + layout = NLog.Layouts.Layout.FromString("${longdate}|${level:uppercase=true}|${callsite}:${callsite-linenumber}|${message}${onexception:${newline}${exception:format=tostring}${exception:format=StackTrace}}"); + } + else + { + layout = NLog.Layouts.Layout.FromString("${longdate}|${message}${onexception:${newline}${exception:format=tostring}${exception:format=StackTrace}}"); + } logConfig.AddTarget("console", new NLog.Targets.ColoredConsoleTarget() { Layout = layout }); logConfig.AddRule(minConsoleLogLevel, NLog.LogLevel.Fatal, "console"); NLog.LogManager.Configuration = logConfig; diff --git a/src/Luban.Server/Source/Program.cs b/src/Luban.Server/Source/Program.cs index 68f7dff..6f8a56b 100644 --- a/src/Luban.Server/Source/Program.cs +++ b/src/Luban.Server/Source/Program.cs @@ -16,6 +16,9 @@ namespace Luban.Server { [Option('p', "port", Required = false, HelpText = "listen port")] public int Port { get; set; } = 8899; + + [Option('l', "loglevel", Required = false, HelpText = "log level. default INFO. avaliable value: TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF")] + public string LogLevel { get; set; } = "INFO"; } private static CommandLineOptions ParseOptions(String[] args) @@ -41,7 +44,7 @@ namespace Luban.Server var options = ParseOptions(args); - Luban.Common.Utils.LogUtil.InitSimpleNLogConfigure(NLog.LogLevel.Info); + Luban.Common.Utils.LogUtil.InitSimpleNLogConfigure(NLog.LogLevel.FromString(options.LogLevel)); System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);