1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
package logger
import (
"fmt"
"io"
"log"
"os"
"path"
"path/filepath"
"sync"
)
type LogLevel int
const (
FatalLevel LogLevel = iota
PanicLevel
ErrorLevel
WarnLevel
NotifyLevel
InfoLevel
DebugLevel
)
func (v LogLevel) String() string {
switch v {
case FatalLevel:
return "Fatal"
case PanicLevel:
return "Panic"
case ErrorLevel:
return "Error"
case WarnLevel:
return "Warning"
case NotifyLevel:
return "Notice"
case InfoLevel:
return "Information"
case DebugLevel:
return "Debug"
default:
return "<undef>"
}
}
func (v LogLevel) LongStr() string {
return v.String()
}
func (v LogLevel) ShortStr() string {
switch v {
case FatalLevel:
return "Fatal"
case PanicLevel:
return "Panic"
case ErrorLevel:
return "Error"
case WarnLevel:
return "Warn"
case NotifyLevel:
return "Notice"
case InfoLevel:
return "Info"
case DebugLevel:
return "Debug"
default:
return "<undef>"
}
}
type Log struct {
log *log.Logger
colored bool
level LogLevel
}
func NewLog(log *log.Logger, colored bool, level LogLevel) *Log {
v := &Log{log: log, colored: colored, level: level}
return v
}
type Logger struct {
sync.RWMutex
logs []*Log
packages []*Package
options FormatOptions
logFile *File
rotateMaxSize int64
rotateMaxCount int
enableSyslog bool
}
func NewLogger() *Logger {
stdout := NewLog(log.New(os.Stdout, "", 0), true, DebugLevel)
logs := []*Log{stdout}
options := FormatOptions{TimeFormat: "2006-01-02T15:04:05.000", LevelLength: LevelShort, PackageLength: 8}
l := &Logger{
logs: logs,
options: options,
rotateMaxSize: 1024 * 1024 * 512,
rotateMaxCount: 3,
}
return l
}
func (v *Logger) Close() error {
v.Lock()
defer v.Unlock()
for _, pack := range v.packages {
pack.Close()
}
v.packages = nil
if v.logFile != nil {
v.logFile.Close()
}
return nil
}
func (v *Logger) SetRotateParams(rotateMaxSize int64, rotateMaxCount int) {
v.Lock()
defer v.Unlock()
v.rotateMaxSize = rotateMaxSize
v.rotateMaxCount = rotateMaxCount
}
func (v *Logger) GetRotateMaxSize() int64 {
v.Lock()
defer v.Unlock()
return v.rotateMaxSize
}
func (v *Logger) GetRotateMaxCount() int {
v.Lock()
defer v.Unlock()
return v.rotateMaxCount
}
/*
func (v *Logger) SetApplicationName(appName string) {
v.Lock()
defer v.Unlock()
v.appName = appName
}
func (v *Logger) GetApplicationName() string {
v.RLock()
defer v.RUnlock()
return v.appName
}
*/
func (v *Logger) EnableSyslog(enable bool) {
v.Lock()
defer v.Unlock()
v.enableSyslog = enable
}
func (v *Logger) GetSyslogEnabled() bool {
v.RLock()
defer v.RUnlock()
return v.enableSyslog
}
func (v *Logger) SetFormatOptions(options FormatOptions) {
v.Lock()
defer v.Unlock()
v.options = options
}
func (v *Logger) GetFormatOptions() FormatOptions {
v.RLock()
defer v.RUnlock()
return v.options
}
func (v *Logger) SetLogFileName(logFilePath string) error {
if path.Ext(logFilePath) == "" {
logFilePath += ".log"
}
fp, err := filepath.Abs(logFilePath)
if err != nil {
return err
}
v.Lock()
defer v.Unlock()
lf := &File{Path: fp}
v.logFile = lf
return nil
}
func (v *Logger) GetLogFileInfo() *File {
v.RLock()
defer v.RUnlock()
return v.logFile
}
func (v *Logger) NewPackageLogger(packageName string, level LogLevel) PackageLog {
v.Lock()
defer v.Unlock()
p := &Package{parent: v, packageName: packageName, level: level}
v.packages = append(v.packages, p)
return p
}
func (v *Logger) ChangePackageLogLevel(packageName string, level LogLevel) error {
var p *Package
for _, item := range v.packages {
if item.packageName == packageName {
p = item
break
}
}
if p != nil {
p.SetLogLevel(level)
} else {
err := fmt.Errorf("Package log %q is not found", packageName)
return err
}
return nil
}
func (v *Logger) AddCustomLog(writer io.Writer, colored bool, level LogLevel) {
v.Lock()
defer v.Unlock()
log := NewLog(log.New(writer, "", 0), colored, level)
v.logs = append(v.logs, log)
}
func (v *Logger) getLogs() []*Log {
v.RLock()
defer v.RUnlock()
lst := []*Log{}
for _, item := range v.logs {
lst = append(lst, item)
}
return lst
}
var (
globalLock sync.RWMutex
lgr *Logger
)
func SetFormatOptions(format FormatOptions) {
globalLock.RLock()
defer globalLock.RUnlock()
lgr.SetFormatOptions(format)
}
func SetRotateParams(rotateMaxSize int64, rotateMaxCount int) {
globalLock.RLock()
defer globalLock.RUnlock()
lgr.SetRotateParams(rotateMaxSize, rotateMaxCount)
}
func NewPackageLogger(module string, level LogLevel) PackageLog {
globalLock.RLock()
defer globalLock.RUnlock()
return lgr.NewPackageLogger(module, level)
}
func ChangePackageLogLevel(packageName string, level LogLevel) error {
globalLock.RLock()
defer globalLock.RUnlock()
return lgr.ChangePackageLogLevel(packageName, level)
}
func SetLogFileName(logFilePath string) error {
globalLock.RLock()
defer globalLock.RUnlock()
return lgr.SetLogFileName(logFilePath)
}
/*
func SetApplicationName(appName string) {
globalLock.RLock()
defer globalLock.RUnlock()
lgr.SetApplicationName(appName)
}
*/
func EnableSyslog(enable bool) {
globalLock.RLock()
defer globalLock.RUnlock()
lgr.EnableSyslog(enable)
}
func AddCustomLog(writer io.Writer, colored bool, level LogLevel) {
globalLock.RLock()
defer globalLock.RUnlock()
lgr.AddCustomLog(writer, colored, level)
}
func FinalizeLogger() error {
var err error
if lgr != nil {
err = lgr.Close()
}
globalLock.Lock()
defer globalLock.Unlock()
lgr = nil
return err
}
func init() {
lgr = NewLogger()
}
|