github.com/Cloud-Foundations/Dominator@v0.3.4/lib/log/serverlogger/api.go (about) 1 package serverlogger 2 3 import ( 4 "flag" 5 "fmt" 6 "io" 7 "log" 8 "regexp" 9 "sync" 10 11 liblog "github.com/Cloud-Foundations/Dominator/lib/log" 12 "github.com/Cloud-Foundations/Dominator/lib/logbuf" 13 "github.com/Cloud-Foundations/Dominator/lib/srpc" 14 ) 15 16 var ( 17 initialLogDebugLevel = flag.Int("initialLogDebugLevel", -1, 18 "initial debug log level") 19 logAtStartup = flag.Bool("logAtStartup", true, 20 "If true, write a log entry at startup") 21 logSubseconds = flag.Bool("logSubseconds", false, 22 "if true, datestamps will have subsecond resolution") 23 24 // Interface check. 25 _ liblog.FullDebugLogger = (*Logger)(nil) 26 ) 27 28 type Logger struct { 29 accessChecker func(method string, authInfo *srpc.AuthInformation) bool 30 circularBuffer *logbuf.LogBuffer 31 flags int 32 level int16 33 maxLevel int16 34 mutex sync.Mutex // Lock everything below. 35 streamers map[*streamerType]struct{} 36 } 37 38 type streamerType struct { 39 debugLevel int16 40 excludeRegex *regexp.Regexp // nil: nothing excluded. Processed after incl. 41 includeRegex *regexp.Regexp // nil: everything included. 42 output chan<- []byte 43 } 44 45 // GetStandardFlags will return the standard flags. 46 func GetStandardFlags() int { 47 flags := log.LstdFlags 48 if *logSubseconds { 49 flags |= log.Lmicroseconds 50 } 51 return flags 52 } 53 54 // New will create a Logger which has an internal log buffer (see the 55 // lib/logbuf package). It implements the log.DebugLogger interface. 56 // By default, the max debug level is -1, meaning all debug logs are dropped 57 // (ignored). 58 // The name of the new logger is given by name. This name is used to remotely 59 // identify the logger for SRPC methods such as Logger.SetDebugLevel. The first 60 // or primary logger should be created with name "" (the empty string). 61 func New(name string) *Logger { 62 return newLogger(name, logbuf.GetStandardOptions(), GetStandardFlags()) 63 } 64 65 // NewWithFlags will create a Logger which has an internal log buffer (see the 66 // lib/logbuf package). It implements the log.DebugLogger interface. 67 // By default, the max debug level is -1, meaning all debug logs are dropped 68 // (ignored). 69 // The name of the new logger is given by name. This name is used to remotely 70 // identify the logger for RPC methods such as Logger.SetDebugLevel. The first 71 // or primary logger should be created with name "" (the empty string). 72 func NewWithFlags(name string, flags int) *Logger { 73 return newLogger(name, logbuf.GetStandardOptions(), flags) 74 } 75 76 // NewWithOptions will create a Logger which has an internal log buffer (see the 77 // lib/logbuf package). It implements the log.DebugLogger interface. 78 // By default, the max debug level is -1, meaning all debug logs are dropped 79 // (ignored). 80 // The name of the new logger is given by name. This name is used to remotely 81 // identify the logger for RPC methods such as Logger.SetDebugLevel. The first 82 // or primary logger should be created with name "" (the empty string). 83 func NewWithOptions(name string, options logbuf.Options, flags int) *Logger { 84 return newLogger(name, options, flags) 85 } 86 87 // Debug will call the Print method if level is less than or equal to the max 88 // debug level for the Logger. 89 func (l *Logger) Debug(level uint8, v ...interface{}) { 90 l.debug(int16(level), v...) 91 } 92 93 // Debugf will call the Printf method if level is less than or equal to the max 94 // debug level for the Logger. 95 func (l *Logger) Debugf(level uint8, format string, v ...interface{}) { 96 l.debugf(int16(level), format, v...) 97 } 98 99 // Debugln will call the Println method if level is less than or equal to the 100 // max debug level for the Logger. 101 func (l *Logger) Debugln(level uint8, v ...interface{}) { 102 l.debugln(int16(level), v...) 103 } 104 105 // GetLevel gets the current maximum debug level. 106 func (l *Logger) GetLevel() int16 { 107 return l.level 108 } 109 110 // Fatal is equivalent to Print() followed by a call to os.Exit(1). 111 func (l *Logger) Fatal(v ...interface{}) { 112 l.fatals(fmt.Sprint(v...)) 113 } 114 115 // Fatalf is equivalent to Printf() followed by a call to os.Exit(1). 116 func (l *Logger) Fatalf(format string, v ...interface{}) { 117 l.fatals(fmt.Sprintf(format, v...)) 118 } 119 120 // Fatalln is equivalent to Println() followed by a call to os.Exit(1). 121 func (l *Logger) Fatalln(v ...interface{}) { 122 l.fatals(fmt.Sprintln(v...)) 123 } 124 125 // Flush flushes the open log file (if one is open). This should only be called 126 // just prior to process termination. The log file is automatically flushed 127 // after short periods of inactivity. 128 func (l *Logger) Flush() error { 129 return l.circularBuffer.Flush() 130 } 131 132 // Panic is equivalent to Print() followed by a call to panic(). 133 func (l *Logger) Panic(v ...interface{}) { 134 l.panics(fmt.Sprint(v...)) 135 } 136 137 // Panicf is equivalent to Printf() followed by a call to panic(). 138 func (l *Logger) Panicf(format string, v ...interface{}) { 139 l.panics(fmt.Sprintf(format, v...)) 140 } 141 142 // Panicln is equivalent to Println() followed by a call to panic(). 143 func (l *Logger) Panicln(v ...interface{}) { 144 l.panics(fmt.Sprintln(v...)) 145 } 146 147 // Print prints to the logger. Arguments are handled in the manner of fmt.Print. 148 func (l *Logger) Print(v ...interface{}) { 149 l.prints(fmt.Sprint(v...)) 150 } 151 152 // Printf prints to the logger. Arguments are handled in the manner of 153 // fmt.Printf. 154 func (l *Logger) Printf(format string, v ...interface{}) { 155 l.prints(fmt.Sprintf(format, v...)) 156 } 157 158 // Println prints to the logger. Arguments are handled in the manner of 159 // fmt.Println. 160 func (l *Logger) Println(v ...interface{}) { 161 l.prints(fmt.Sprintln(v...)) 162 } 163 164 // SetAccessChecker sets the function that is called when SRPC methods are 165 // called for the Logger. This allows the application to control which users or 166 // groups are permitted to remotely control the Logger. 167 func (l *Logger) SetAccessChecker( 168 accessChecker func(method string, authInfo *srpc.AuthInformation) bool) { 169 l.accessChecker = accessChecker 170 } 171 172 // SetLevel sets the maximum debug level. A negative level will cause all debug 173 // messages to be dropped. 174 func (l *Logger) SetLevel(maxLevel int16) { 175 l.setLevel(maxLevel) 176 } 177 178 // WriteHtml will write the contents of the internal log buffer to writer, with 179 // appropriate HTML markups. 180 func (l *Logger) WriteHtml(writer io.Writer) { 181 l.writeHtml(writer) 182 }