package Logger import ( "fmt" "path/filepath" "path" "gopkg.in/natefinch/lumberjack.v2" "time" "git.swzry.com/NSMCServerLauncher/Utils" ) type LogFileWriter struct{ lumberjackLogger lumberjack.Logger withTime bool prefix string enable bool time_format_string string } func (this *LogFileWriter) Write(data []byte) (int, error) { if(this.enable){ if(this.withTime){ this.lumberjackLogger.Write([]byte(fmt.Sprintf("[%s]%s",time.Now().Format(this.time_format_string),this.prefix))) }else { if this.prefix != ""{ this.lumberjackLogger.Write([]byte(this.prefix)) } } return this.lumberjackLogger.Write(data) }else { return len(data),nil } } var Log TypeOfLoggerSystem type TypeOfLoggerSystem struct { SysDebug LogFileWriter SysInfo LogFileWriter SysWarning LogFileWriter SysCritical LogFileWriter SysFatal LogFileWriter MCLOut LogFileWriter MCLErr LogFileWriter SSH LogFileWriter } func InitLogger() error { logdir,err := filepath.Abs(LoggerConf.logdir) if err != nil { fmt.Println("[StdOutLog][Fatal Error] Failed Get Log Dir: ",err) return err } if err := Utils.MkDirIfNotExist(logdir); err != nil {return err} syslbj := lumberjack.Logger{ Filename: path.Join(LoggerConf.logdir,LoggerConf.syslogcfg.Filename), MaxSize: LoggerConf.syslogcfg.MaxSize, MaxAge: LoggerConf.syslogcfg.MaxAge, MaxBackups: LoggerConf.syslogcfg.MaxBackups, LocalTime: LoggerConf.syslogcfg.LocalTime, Compress: LoggerConf.syslogcfg.Compress, } mcoutlbj := lumberjack.Logger{ Filename: path.Join(LoggerConf.logdir,LoggerConf.mcloutlogcfg.Filename), MaxSize: LoggerConf.mcloutlogcfg.MaxSize, MaxAge: LoggerConf.mcloutlogcfg.MaxAge, MaxBackups: LoggerConf.mcloutlogcfg.MaxBackups, LocalTime: LoggerConf.mcloutlogcfg.LocalTime, Compress: LoggerConf.mcloutlogcfg.Compress, } mcerrlbj := lumberjack.Logger{ Filename: path.Join(LoggerConf.logdir,LoggerConf.mclerrlogcfg.Filename), MaxSize: LoggerConf.mclerrlogcfg.MaxSize, MaxAge: LoggerConf.mclerrlogcfg.MaxAge, MaxBackups: LoggerConf.mclerrlogcfg.MaxBackups, LocalTime: LoggerConf.mclerrlogcfg.LocalTime, Compress: LoggerConf.mclerrlogcfg.Compress, } sshlbj := lumberjack.Logger{ Filename: path.Join(LoggerConf.logdir,LoggerConf.sshlogcfg.Filename), MaxSize: LoggerConf.sshlogcfg.MaxSize, MaxAge: LoggerConf.sshlogcfg.MaxAge, MaxBackups: LoggerConf.sshlogcfg.MaxBackups, LocalTime: LoggerConf.sshlogcfg.LocalTime, Compress: LoggerConf.sshlogcfg.Compress, } Log = TypeOfLoggerSystem{ SysCritical: LogFileWriter{ lumberjackLogger:syslbj, enable:true, prefix:"", withTime: true, time_format_string:LoggerConf.time_format, }, SysFatal: LogFileWriter{ lumberjackLogger:syslbj, enable:true, prefix:"", withTime: true, time_format_string:LoggerConf.time_format, }, SysInfo: LogFileWriter{ lumberjackLogger:syslbj, enable:true, prefix:"", withTime: true, time_format_string:LoggerConf.time_format, }, SysWarning: LogFileWriter{ lumberjackLogger:syslbj, enable:true, prefix:"", withTime: true, time_format_string:LoggerConf.time_format, }, SysDebug: LogFileWriter{ lumberjackLogger:syslbj, enable:LoggerConf.debug_log, prefix:"", withTime: true, time_format_string:LoggerConf.time_format, }, MCLOut: LogFileWriter{ lumberjackLogger:mcoutlbj, enable:true, prefix:"", withTime: false, time_format_string:LoggerConf.time_format, }, MCLErr: LogFileWriter{ lumberjackLogger:mcerrlbj, enable:true, prefix:"", withTime: false, time_format_string:LoggerConf.time_format, }, SSH: LogFileWriter{ lumberjackLogger:sshlbj, enable:true, prefix:"", withTime: true, time_format_string:LoggerConf.time_format, }, } return nil }