overhaul with logger using logrus
Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
parent
6ac1e7e406
commit
cb2095dfac
|
@ -0,0 +1 @@
|
|||
go.sum
|
6
go.mod
6
go.mod
|
@ -1,3 +1,9 @@
|
|||
module git.0cd.xyz/michael/gtools
|
||||
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/magefile/mage v1.11.0 // indirect
|
||||
github.com/sirupsen/logrus v1.8.0
|
||||
golang.org/x/sys v0.0.0-20210305230114-8fe3ee5dd75b // indirect
|
||||
)
|
||||
|
|
100
logger/logger.go
100
logger/logger.go
|
@ -1,49 +1,66 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"html"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"git.0cd.xyz/michael/gtools/colour"
|
||||
"git.0cd.xyz/michael/gtools/nettools"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Logger Options
|
||||
type Logger struct {
|
||||
Error *File
|
||||
Access *File
|
||||
}
|
||||
|
||||
// File log file
|
||||
type File struct {
|
||||
Name string
|
||||
File *os.File
|
||||
Error *logrus.Logger
|
||||
Access *logrus.Logger
|
||||
}
|
||||
|
||||
// New instance of logger
|
||||
func New(path string) (logger *Logger, err error) {
|
||||
errorlog, err := open(path + "error.log")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func New(path, mode string, level int) *Logger {
|
||||
var access, erro io.Writer
|
||||
if mode == "file" {
|
||||
errorlog, err := open(path + "error.log")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
accesslog, err := open(path + "access.log")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
erro = io.MultiWriter(errorlog, os.Stderr)
|
||||
access = io.MultiWriter(accesslog, os.Stdout)
|
||||
} else {
|
||||
erro, access = os.Stderr, os.Stderr
|
||||
}
|
||||
accesslog, err := open(path + "access.log")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logger = &Logger{
|
||||
Error: &File{
|
||||
Name: "Error",
|
||||
File: errorlog,
|
||||
return &Logger{
|
||||
Error: &logrus.Logger{
|
||||
Out: erro,
|
||||
Hooks: make(logrus.LevelHooks),
|
||||
Formatter: &logrus.TextFormatter{
|
||||
ForceColors: true,
|
||||
FullTimestamp: true,
|
||||
TimestampFormat: "2006-01-02 15:04:05",
|
||||
},
|
||||
Level: logrus.Level(level),
|
||||
},
|
||||
Access: &File{
|
||||
Name: "Access",
|
||||
File: accesslog,
|
||||
Access: &logrus.Logger{
|
||||
Out: access,
|
||||
Hooks: make(logrus.LevelHooks),
|
||||
Formatter: &logrus.TextFormatter{
|
||||
ForceColors: true,
|
||||
FullTimestamp: true,
|
||||
TimestampFormat: "2006-01-02 15:04:05",
|
||||
},
|
||||
Level: logrus.InfoLevel,
|
||||
},
|
||||
}
|
||||
return logger, nil
|
||||
}
|
||||
|
||||
// open log file
|
||||
func open(path string) (*os.File, error) {
|
||||
dir, _ := filepath.Split(path)
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
|
@ -57,30 +74,15 @@ func open(path string) (*os.File, error) {
|
|||
return file, nil
|
||||
}
|
||||
|
||||
// Write to stderr and log file
|
||||
func (f *File) Write(err error) {
|
||||
log.SetOutput(io.MultiWriter(os.Stderr, f.File))
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
// SlientWrite write to file with no terminal output
|
||||
func (f *File) SlientWrite(err error) {
|
||||
log.SetOutput(f.File)
|
||||
}
|
||||
|
||||
// Read from log file
|
||||
func (f *File) Read() (file []byte, err error) {
|
||||
file, err = ioutil.ReadFile(f.File.Name())
|
||||
// AccessMessage client access message
|
||||
func (logger *Logger) AccessMessage(r *http.Request, statuscode int) {
|
||||
ip, err := nettools.IP(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
logger.Error.Warn(err)
|
||||
}
|
||||
return file, err
|
||||
}
|
||||
|
||||
// Close log file
|
||||
func (f *File) Close() error {
|
||||
if err := f.File.Close(); err != nil {
|
||||
return err
|
||||
hostname, err := nettools.Hostname(ip)
|
||||
if err != nil {
|
||||
logger.Error.Warn(err)
|
||||
}
|
||||
return nil
|
||||
logger.Access.Infof("%s %v - %s %s %s", ip, hostname, r.Method, colour.Status(statuscode), html.EscapeString(r.URL.Path))
|
||||
}
|
||||
|
|
|
@ -2,16 +2,11 @@ package nettools
|
|||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
"log"
|
||||
"math/big"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"git.0cd.xyz/michael/gtools/colour"
|
||||
"git.0cd.xyz/michael/gtools/logger"
|
||||
)
|
||||
|
||||
// IP returns IP address from requester
|
||||
|
@ -67,19 +62,3 @@ func Hostname(ip net.IP) ([]string, error) {
|
|||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// AccessMessage returns client access message
|
||||
func AccessMessage(r *http.Request, statuscode int, logger *logger.Logger) {
|
||||
ip, err := IP(r)
|
||||
if err != nil {
|
||||
logger.Error.SlientWrite(err)
|
||||
return
|
||||
}
|
||||
hostname, err := Hostname(ip)
|
||||
if err != nil {
|
||||
logger.Error.SlientWrite(err)
|
||||
return
|
||||
}
|
||||
out := fmt.Sprintf("%s %v - %s %s %s", ip, hostname, r.Method, colour.Status(statuscode), html.EscapeString(r.URL.Path))
|
||||
logger.Access.Write(errors.New(out))
|
||||
}
|
||||
|
|
Reference in New Issue