-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
200 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/kyallanum/athena/cmd" | ||
"github.com/kyallanum/athena/models/logger" | ||
) | ||
|
||
func main() { | ||
logger := logger.New() | ||
|
||
defer func() { | ||
if err := recover(); err != nil { | ||
fmt.Fprintln(os.Stderr, "\nAn error occured: ", err) | ||
logger.Fatalf("\nAn error occured: \n\t%s", err) | ||
} | ||
}() | ||
|
||
if err := cmd.Execute(); err != nil { | ||
if err := cmd.Execute(logger); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package logger | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type FileFormatter struct { | ||
logrus.TextFormatter | ||
TimestampFormat string | ||
} | ||
|
||
func (formatter *FileFormatter) Format(entry *logrus.Entry) ([]byte, error) { | ||
timestamp := entry.Time.Format(formatter.TimestampFormat) | ||
return []byte(fmt.Sprintf("%s %s: %s\n", timestamp, entry.Level.String(), entry.Message)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package logger | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type FormatterHook struct { | ||
Writer io.Writer | ||
LogLevels []logrus.Level | ||
Formatter logrus.Formatter | ||
} | ||
|
||
func (hook *FormatterHook) Fire(entry *logrus.Entry) error { | ||
line, err := hook.Formatter.Format(entry) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = hook.Writer.Write(line) | ||
return err | ||
} | ||
|
||
func (hook *FormatterHook) Levels() []logrus.Level { | ||
return hook.LogLevels | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package logger | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func AddFileLogger(logger *logrus.Logger, fileName string) { | ||
file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) | ||
if err != nil { | ||
logger.Fatalf("An error occurred: \n\t%s", err.Error()) | ||
} | ||
defer file.Close() | ||
|
||
FileHook := &FormatterHook{ | ||
Writer: file, | ||
LogLevels: []logrus.Level{ | ||
logrus.InfoLevel, | ||
logrus.DebugLevel, | ||
logrus.WarnLevel, | ||
logrus.ErrorLevel, | ||
logrus.FatalLevel, | ||
logrus.PanicLevel, | ||
}, | ||
Formatter: &FileFormatter{ | ||
TimestampFormat: "01/02/2006 15:03:04", | ||
}, | ||
} | ||
|
||
logger.AddHook(FileHook) | ||
} | ||
|
||
func New(fileName ...string) *logrus.Logger { | ||
newLogger := logrus.New() | ||
newLogger.SetOutput(io.Discard) | ||
newLogger.SetLevel(logrus.InfoLevel) | ||
|
||
StdoutHook := &FormatterHook{ | ||
Writer: os.Stdout, | ||
LogLevels: []logrus.Level{ | ||
logrus.InfoLevel, | ||
}, | ||
Formatter: &StdoutFormatter{}, | ||
} | ||
newLogger.AddHook(StdoutHook) | ||
|
||
StderrHook := &FormatterHook{ | ||
Writer: os.Stderr, | ||
LogLevels: []logrus.Level{ | ||
logrus.ErrorLevel, | ||
logrus.FatalLevel, | ||
logrus.PanicLevel, | ||
}, | ||
Formatter: &StderrFormatter{ | ||
TimestampFormat: "01/02/2006 15:04:05", | ||
}, | ||
} | ||
newLogger.AddHook(StderrHook) | ||
|
||
return newLogger | ||
} |
Oops, something went wrong.