logging #1
3 changed files with 28 additions and 15 deletions
1
awl.go
1
awl.go
|
@ -10,7 +10,6 @@ import (
|
|||
func main() {
|
||||
logger := logawl.New()
|
||||
logger.Println("Test PLEASE WORK")
|
||||
logger.Println("Test 2")
|
||||
app := prepareCLI()
|
||||
err := app.Run(os.Args)
|
||||
if err != nil {
|
||||
|
|
|
@ -2,23 +2,22 @@ package logawl
|
|||
|
||||
import "fmt"
|
||||
|
||||
type Lvl uint32
|
||||
type Lvl int
|
||||
|
||||
type Level struct {
|
||||
lvl uint32
|
||||
lvl int
|
||||
Prefix string
|
||||
}
|
||||
|
||||
func (l *Level) GetLevel(i uint32) (string, error) {
|
||||
i = l.lvl
|
||||
func (l *Level) GetLevel(i int) (string, error) {
|
||||
switch i {
|
||||
case uint32(FatalLevel):
|
||||
case int(FatalLevel):
|
||||
return "FATAL ", nil
|
||||
case uint32(ErrorLevel):
|
||||
case int(ErrorLevel):
|
||||
return "ERROR ", nil
|
||||
case uint32(InfoLevel):
|
||||
case int(InfoLevel):
|
||||
return "INFO ", nil
|
||||
case uint32(DebugLevel):
|
||||
case int(DebugLevel):
|
||||
return "DEBUG ", nil
|
||||
}
|
||||
return "", fmt.Errorf("Invalid log level choice")
|
||||
|
|
|
@ -18,6 +18,9 @@ type Logger struct {
|
|||
isDiscard int32
|
||||
}
|
||||
|
||||
// Calling New instantiates Logger
|
||||
//
|
||||
// Level can be changed to one of the other log levels (FatalLevel, ErrorLevel, InfoLevel, DebugLevel)
|
||||
func New() *Logger {
|
||||
return &Logger{
|
||||
Out: os.Stderr,
|
||||
|
@ -26,13 +29,15 @@ func New() *Logger {
|
|||
}
|
||||
}
|
||||
|
||||
// Takes any and prints it out to Logger -> Out (io.Writer (default is std.Err))
|
||||
func (l *Logger) Println(v ...any) {
|
||||
if atomic.LoadInt32(&l.isDiscard) != 0 {
|
||||
return
|
||||
}
|
||||
switch l.Level {
|
||||
switch l.Level { //Goes through log levels and does stuff based on them (Fatal os.Exit...etc)
|
||||
case 0:
|
||||
l.Printer(0, fmt.Sprintln(v...))
|
||||
os.Exit(1)
|
||||
case 1:
|
||||
l.Printer(1, fmt.Sprintln(v...))
|
||||
case 2:
|
||||
|
@ -43,24 +48,33 @@ func (l *Logger) Println(v ...any) {
|
|||
|
||||
}
|
||||
|
||||
// Formats the log header as such <LogLevel> YYYY/MM/DD HH:MM:SS (local time) <the message to log>
|
||||
func (l *Logger) formatHeader(buf *[]byte, t time.Time, level int, line int) {
|
||||
var lv Level
|
||||
if logLvl, err := lv.GetLevel(uint32(level)); err != nil {
|
||||
if logLvl, err := lv.GetLevel(level); err != nil {
|
||||
fmt.Printf("Invalid log level %s. Error: %v", logLvl, err)
|
||||
os.Exit(1)
|
||||
} else {
|
||||
*buf = append(*buf, logLvl...)
|
||||
year, month, day := t.Date()
|
||||
itoa(buf, year, 4)
|
||||
formatter(buf, year, 4)
|
||||
*buf = append(*buf, '/')
|
||||
itoa(buf, int(month), 2)
|
||||
formatter(buf, int(month), 2)
|
||||
*buf = append(*buf, '/')
|
||||
itoa(buf, day, 2)
|
||||
formatter(buf, day, 2)
|
||||
*buf = append(*buf, ' ')
|
||||
hour, min, sec := t.Clock()
|
||||
formatter(buf, hour, 2)
|
||||
*buf = append(*buf, ':')
|
||||
formatter(buf, min, 2)
|
||||
*buf = append(*buf, ':')
|
||||
formatter(buf, sec, 2)
|
||||
*buf = append(*buf, ' ')
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Printer prints the formatted message directly to stdErr
|
||||
func (l *Logger) Printer(level int, s string) error {
|
||||
now := time.Now()
|
||||
var line int
|
||||
|
@ -80,8 +94,9 @@ func (l *Logger) Printer(level int, s string) error {
|
|||
// Some line formatting stuff from Golang log stdlib file
|
||||
//
|
||||
// Please view https://cs.opensource.google/go/go/+/refs/tags/go1.18.3:src/log/log.go;drc=41e1d9075e428c2fc32d966b3752a3029b620e2c;l=96
|
||||
//
|
||||
// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding.
|
||||
func itoa(buf *[]byte, i int, wid int) {
|
||||
func formatter(buf *[]byte, i int, wid int) {
|
||||
// Assemble decimal in reverse order.
|
||||
var b [20]byte
|
||||
bp := len(b) - 1
|
||||
|
|
Loading…
Reference in a new issue