github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/logger/log.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package logger 18 19 import ( 20 "fmt" 21 "io" 22 "os" 23 24 "github.com/ethereumproject/go-ethereum/common" 25 ) 26 27 func openFile(datadir string, filename string) *os.File { 28 path := common.EnsurePathAbsoluteOrRelativeTo(datadir, filename) 29 file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) 30 if err != nil { 31 panic(fmt.Sprintf("error opening log file '%s': %v", filename, err)) 32 } 33 return file 34 } 35 36 func New(datadir string, logFile string, logLevel int, flags int) LogSystem { 37 var writer io.Writer 38 if logFile == "" { 39 writer = os.Stdout 40 } else { 41 writer = openFile(datadir, logFile) 42 } 43 44 var sys LogSystem 45 sys = NewStdLogSystem(writer, flags, LogLevel(logLevel)) 46 AddLogSystem(sys) 47 48 return sys 49 } 50 51 func BuildNewMLogSystem(datadir string, logFile string, logLevel int, flags int, withTimestamp bool) LogSystem { 52 var writer io.Writer 53 if logFile == "" { 54 writer = os.Stdout 55 } else { 56 writer = openFile(datadir, logFile) 57 } 58 59 var sys LogSystem 60 sys = NewMLogSystem(writer, flags, LogLevel(logLevel), withTimestamp) 61 AddLogSystem(sys) 62 63 return sys 64 } 65 66 func NewJSONsystem(datadir string, logFile string) LogSystem { 67 var writer io.Writer 68 if logFile == "-" { 69 writer = os.Stdout 70 } else { 71 writer = openFile(datadir, logFile) 72 } 73 74 var sys LogSystem 75 sys = NewJsonLogSystem(writer) 76 AddLogSystem(sys) 77 78 return sys 79 } 80 81 const ( 82 reset = "\x1b[39m" 83 green = "\x1b[32m" 84 blue = "\x1b[36m" 85 yellow = "\x1b[33m" 86 red = "\x1b[31m" 87 magenta = "\x1b[35m" 88 ) 89 90 func ColorGreen(s string) (coloredString string) { 91 return green + s + reset 92 } 93 func ColorRed(s string) (coloredString string) { 94 return red + s + reset 95 } 96 func ColorBlue(s string) (coloredString string) { 97 return blue + s + reset 98 } 99 func ColorYellow(s string) (coloredString string) { 100 return yellow + s + reset 101 } 102 func ColorMagenta(s string) (coloredString string) { 103 return magenta + s + reset 104 }