github.com/google/martian/v3@v3.3.3/log/log.go (about) 1 // Copyright 2015 Google Inc. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package log provides a universal logger for martian packages. 16 package log 17 18 import ( 19 "fmt" 20 "log" 21 "sync" 22 ) 23 24 const ( 25 // Silent is a level that logs nothing. 26 Silent int = iota 27 // Error is a level that logs error logs. 28 Error 29 // Info is a level that logs error, and info logs. 30 Info 31 // Debug is a level that logs error, info, and debug logs. 32 Debug 33 ) 34 35 // Default log level is Error. 36 var ( 37 level = Error 38 lock sync.Mutex 39 currLogger Logger = &logger{} 40 ) 41 42 type Logger interface { 43 Infof(format string, args ...interface{}) 44 Debugf(format string, args ...interface{}) 45 Errorf(format string, args ...interface{}) 46 } 47 48 // SetLogger changes the default logger. This must be called very first, 49 // before interacting with rest of the martian package. Changing it at 50 // runtime is not supported. 51 func SetLogger(l Logger) { 52 currLogger = l 53 } 54 55 // SetLevel sets the global log level. 56 func SetLevel(l int) { 57 lock.Lock() 58 defer lock.Unlock() 59 60 level = l 61 } 62 63 // Infof logs an info message. 64 func Infof(format string, args ...interface{}) { 65 currLogger.Infof(format, args...) 66 } 67 68 // Debugf logs a debug message. 69 func Debugf(format string, args ...interface{}) { 70 currLogger.Debugf(format, args...) 71 } 72 73 // Errorf logs an error message. 74 func Errorf(format string, args ...interface{}) { 75 currLogger.Errorf(format, args...) 76 } 77 78 type logger struct{} 79 80 func (l *logger) Infof(format string, args ...interface{}) { 81 lock.Lock() 82 defer lock.Unlock() 83 84 if level < Info { 85 return 86 } 87 88 msg := fmt.Sprintf("INFO: %s", format) 89 if len(args) > 0 { 90 msg = fmt.Sprintf(msg, args...) 91 } 92 93 log.Println(msg) 94 } 95 96 func (l *logger) Debugf(format string, args ...interface{}) { 97 lock.Lock() 98 defer lock.Unlock() 99 100 if level < Debug { 101 return 102 } 103 104 msg := fmt.Sprintf("DEBUG: %s", format) 105 if len(args) > 0 { 106 msg = fmt.Sprintf(msg, args...) 107 } 108 109 log.Println(msg) 110 } 111 112 func (l *logger) Errorf(format string, args ...interface{}) { 113 lock.Lock() 114 defer lock.Unlock() 115 116 if level < Error { 117 return 118 } 119 120 msg := fmt.Sprintf("ERROR: %s", format) 121 if len(args) > 0 { 122 msg = fmt.Sprintf(msg, args...) 123 } 124 125 log.Println(msg) 126 }