Skip to content

Commit

Permalink
Make mlog instance accessible
Browse files Browse the repository at this point in the history
- Allow direct access to the mlog struct for access to specific loggers
  • Loading branch information
nochso committed May 1, 2016
1 parent b4634a5 commit 81f7e81
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions mlog.go
Expand Up @@ -44,7 +44,7 @@ type mlog struct {
LogFile *RotatingFileHandler
}

var logger mlog
var Logger mlog

// DefaultFlags used by created loggers
var DefaultFlags = log.Ldate | log.Ltime | log.Lshortfile
Expand Down Expand Up @@ -140,8 +140,8 @@ func StartEx(level LogLevel, path string, maxBytes, backupCount int) {

// Stop stops the logging
func Stop() error {
if logger.LogFile != nil {
return logger.LogFile.Close()
if Logger.LogFile != nil {
return Logger.LogFile.Close()
}

return nil
Expand All @@ -151,8 +151,8 @@ func Stop() error {
//Typically, this means flushing the file system's in-memory copy
//of recently written data to disk.
func Sync() {
if logger.LogFile != nil {
logger.LogFile.fd.Sync()
if Logger.LogFile != nil {
Logger.LogFile.fd.Sync()
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ func doLogging(logLevel LogLevel, fileName string, maxBytes, backupCount int) {
}
}

logger = mlog{
Logger = mlog{
Trace: log.New(traceHandle, "T: ", DefaultFlags),
Info: log.New(infoHandle, "I: ", DefaultFlags),
Warning: log.New(warnHandle, "W: ", DefaultFlags),
Expand All @@ -217,56 +217,56 @@ func doLogging(logLevel LogLevel, fileName string, maxBytes, backupCount int) {
LogFile: fileHandle,
}

atomic.StoreInt32(&logger.LogLevel, int32(logLevel))
atomic.StoreInt32(&Logger.LogLevel, int32(logLevel))
}

//** TRACE

// Trace writes to the Trace destination
func Trace(format string, a ...interface{}) {
logger.Trace.Output(2, fmt.Sprintf(format, a...))
Logger.Trace.Output(2, fmt.Sprintf(format, a...))
}

//** INFO

// Info writes to the Info destination
func Info(format string, a ...interface{}) {
logger.Info.Output(2, fmt.Sprintf(format, a...))
Logger.Info.Output(2, fmt.Sprintf(format, a...))
}

//** WARNING

// Warning writes to the Warning destination
func Warning(format string, a ...interface{}) {
logger.Warning.Output(2, fmt.Sprintf(format, a...))
Logger.Warning.Output(2, fmt.Sprintf(format, a...))
}

//** ERROR

// Error writes to the Error destination and accepts an err
func Error(err error) {
logger.Error.Output(2, fmt.Sprintf("%s\n", err))
Logger.Error.Output(2, fmt.Sprintf("%s\n", err))
}

// IfError is a shortcut function for log.Error if error
func IfError(err error) {
if err != nil {
logger.Error.Output(2, fmt.Sprintf("%s\n", err))
Logger.Error.Output(2, fmt.Sprintf("%s\n", err))
}
}

//** FATAL

// Fatal writes to the Fatal destination and exits with an error 255 code
func Fatal(a ...interface{}) {
logger.Fatal.Output(2, fmt.Sprint(a...))
Logger.Fatal.Output(2, fmt.Sprint(a...))
Sync()
os.Exit(255)
}

// Fatalf writes to the Fatal destination and exits with an error 255 code
func Fatalf(format string, a ...interface{}) {
logger.Fatal.Output(2, fmt.Sprintf(format, a...))
Logger.Fatal.Output(2, fmt.Sprintf(format, a...))
Sync()
os.Exit(255)
}
Expand All @@ -275,7 +275,7 @@ func Fatalf(format string, a ...interface{}) {
// exits with an error 255 code
func FatalIfError(err error) {
if err != nil {
logger.Fatal.Output(2, fmt.Sprintf("%s\n", err))
Logger.Fatal.Output(2, fmt.Sprintf("%s\n", err))
Sync()
os.Exit(255)
}
Expand Down

0 comments on commit 81f7e81

Please sign in to comment.