github.com/go-spring/spring-base@v1.1.3/log/log_level.go (about) 1 /* 2 * Copyright 2012-2019 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package log 18 19 import ( 20 "fmt" 21 "strings" 22 ) 23 24 const ( 25 NoneLevel = Level(iota) 26 TraceLevel 27 DebugLevel 28 InfoLevel 29 WarnLevel 30 ErrorLevel 31 PanicLevel 32 FatalLevel 33 OffLevel 34 ) 35 36 // Level used for identifying the severity of an event. 37 type Level int32 38 39 func (level Level) String() string { 40 switch level { 41 case NoneLevel: 42 return "NONE" 43 case TraceLevel: 44 return "TRACE" 45 case DebugLevel: 46 return "DEBUG" 47 case InfoLevel: 48 return "INFO" 49 case WarnLevel: 50 return "WARN" 51 case ErrorLevel: 52 return "ERROR" 53 case PanicLevel: 54 return "PANIC" 55 case FatalLevel: 56 return "FATAL" 57 case OffLevel: 58 return "OFF" 59 default: 60 return "INVALID" 61 } 62 } 63 64 // ParseLevel parses string to a level, and returns error if the conversion fails. 65 func ParseLevel(str string) (Level, error) { 66 switch strings.ToUpper(str) { 67 case "NONE": 68 return NoneLevel, nil 69 case "TRACE": 70 return TraceLevel, nil 71 case "DEBUG": 72 return DebugLevel, nil 73 case "INFO": 74 return InfoLevel, nil 75 case "WARN": 76 return WarnLevel, nil 77 case "ERROR": 78 return ErrorLevel, nil 79 case "PANIC": 80 return PanicLevel, nil 81 case "FATAL": 82 return FatalLevel, nil 83 case "OFF": 84 return OffLevel, nil 85 default: 86 return -1, fmt.Errorf("invalid level %s", str) 87 } 88 }