gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/log/root.go (about) 1 // Copyright 2018 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The aquachain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package log 18 19 import ( 20 "os" 21 ) 22 23 var ( 24 root = &logger{[]interface{}{}, new(swapHandler)} 25 StdoutHandler = StreamHandler(os.Stdout, LogfmtFormat()) 26 StderrHandler = StreamHandler(os.Stderr, LogfmtFormat()) 27 ) 28 29 func init() { 30 root.SetHandler(DiscardHandler()) 31 } 32 33 // New returns a new logger with the given context. 34 // New is a convenient alias for Root().New 35 func New(ctx ...interface{}) Logger { 36 return root.New(ctx...) 37 } 38 39 // Root returns the root logger 40 func Root() Logger { 41 return root 42 } 43 44 // The following functions bypass the exported logger methods (logger.Debug, 45 // etc.) to keep the call depth the same for all paths to logger.write so 46 // runtime.Caller(2) always refers to the call site in client code. 47 48 // Trace is a convenient alias for Root().Trace 49 func Trace(msg string, ctx ...interface{}) { 50 root.write(msg, LvlTrace, ctx) 51 } 52 53 // Debug is a convenient alias for Root().Debug 54 func Debug(msg string, ctx ...interface{}) { 55 root.write(msg, LvlDebug, ctx) 56 } 57 58 // Info is a convenient alias for Root().Info 59 func Info(msg string, ctx ...interface{}) { 60 root.write(msg, LvlInfo, ctx) 61 } 62 63 // Warn is a convenient alias for Root().Warn 64 func Warn(msg string, ctx ...interface{}) { 65 root.write(msg, LvlWarn, ctx) 66 } 67 68 // Error is a convenient alias for Root().Error 69 func Error(msg string, ctx ...interface{}) { 70 root.write(msg, LvlError, ctx) 71 } 72 73 // Crit is a convenient alias for Root().Crit 74 func Crit(msg string, ctx ...interface{}) { 75 root.write(msg, LvlCrit, ctx) 76 os.Exit(1) 77 }