From d83a0129678744e566f2abd2eec4e2991dfad6ef Mon Sep 17 00:00:00 2001 From: Ge Song Date: Tue, 25 Apr 2023 15:03:33 +0800 Subject: [PATCH] =?UTF-8?q?log/=E6=96=B0=E5=A2=9EParseLevel;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log/logsdk/level.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/log/logsdk/level.go b/log/logsdk/level.go index 774948b..ae56306 100644 --- a/log/logsdk/level.go +++ b/log/logsdk/level.go @@ -1,6 +1,8 @@ package logsdk -import "fmt" +import ( + "fmt" +) // Level 日志等级 type Level int @@ -12,6 +14,30 @@ func (level Level) String() string { return "unknown" } +// ParseLevel 把 level 字符串解析成 Level, +// 支持的字符串: panic, fatal, error, warn, info, debug, trace, +// 传入不支持的字符串返回 LevelInfo. +func ParseLevel(s string) Level { + switch s { + case LevelPanicValue: + return LevelPanic + case LevelFatalValue: + return LevelFatal + case LevelErrorValue: + return LevelError + case LevelWarnValue: + return LevelWarn + case LevelInfoValue: + return LevelInfo + case LevelDebugValue: + return LevelDebug + case LevelTraceValue: + return LevelTrace + default: + return LevelInfo + } +} + func (level Level) MarshalText() ([]byte, error) { switch level { case LevelPanic: