Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/util/log/logger.go
Chris MacLeod 14378fbf39 Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds

The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.

* Style changes and old code removal in router

Router needs a lot of work done to its (lack of) error handling.

* Dead code removal and style changes

Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.

* Finish dead code removal and style changes

Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.

* Fix accidental find-and-replace

* Style, error checking, saftey, bug fix changes

* Redo error checking erased during merge

* Re-add merge-erased fix. Make Safe safe.
2017-05-09 21:34:40 -05:00

133 lignes
3 Kio
Go

package log
import (
"net/http"
"os"
"github.com/Sirupsen/logrus"
"github.com/ewhal/nyaa/config"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)
func LumberJackLogger(filePath string, maxSize int, maxBackups int, maxAge int) *lumberjack.Logger {
return &lumberjack.Logger{
Filename: filePath,
MaxSize: maxSize, // megabytes
MaxBackups: maxBackups,
MaxAge: maxAge, //days
}
}
func InitLogToStdoutDebug() {
logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true})
logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.DebugLevel)
}
func InitLogToStdout() {
logrus.SetFormatter(&logrus.TextFormatter{})
logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.WarnLevel)
}
func InitLogToFile() {
logrus.SetFormatter(&logrus.JSONFormatter{})
out := LumberJackLogger(config.ErrorLogFilePath+config.ErrorLogFileExtension, config.ErrorLogMaxSize, config.ErrorLogMaxBackups, config.ErrorLogMaxAge)
logrus.SetOutput(out)
logrus.SetLevel(logrus.WarnLevel)
}
// Init logrus
func Init(environment string) {
switch environment {
case "DEVELOPMENT":
InitLogToStdoutDebug()
case "TEST":
InitLogToFile()
case "PRODUCTION":
InitLogToFile()
}
logrus.Debugf("Environment : %s", environment)
}
// Debug logs a message with debug log level.
func Debug(msg string) {
logrus.Debug(msg)
}
// Debugf logs a formatted message with debug log level.
func Debugf(msg string, args ...interface{}) {
logrus.Debugf(msg, args...)
}
// Info logs a message with info log level.
func Info(msg string) {
logrus.Info(msg)
}
// Infof logs a formatted message with info log level.
func Infof(msg string, args ...interface{}) {
logrus.Infof(msg, args...)
}
// Warn logs a message with warn log level.
func Warn(msg string) {
logrus.Warn(msg)
}
// Warnf logs a formatted message with warn log level.
func Warnf(msg string, args ...interface{}) {
logrus.Warnf(msg, args...)
}
// Error logs a message with error log level.
func Error(msg string) {
logrus.Error(msg)
}
// Errorf logs a formatted message with error log level.
func Errorf(msg string, args ...interface{}) {
logrus.Errorf(msg, args...)
}
// Fatal logs a message with fatal log level.
func Fatal(msg string) {
logrus.Fatal(msg)
}
// Fatalf logs a formatted message with fatal log level.
func Fatalf(msg string, args ...interface{}) {
logrus.Fatalf(msg, args...)
}
// Panic logs a message with panic log level.
func Panic(msg string) {
logrus.Panic(msg)
}
// Panicf logs a formatted message with panic log level.
func Panicf(msg string, args ...interface{}) {
logrus.Panicf(msg, args...)
}
// log response body data for debugging
func DebugResponse(response *http.Response) string {
bodyBuffer := make([]byte, 5000)
var str string
count, err := response.Body.Read(bodyBuffer)
if err != nil {
Debug(err.Error())
return ""
}
for ; count > 0; count, err = response.Body.Read(bodyBuffer) {
if err != nil {
Debug(err.Error())
continue
}
str += string(bodyBuffer[:count])
}
Debugf("response data : %v", str)
return str
}