This commit is contained in:
程广 2023-01-23 09:59:30 +08:00
parent f658dc4cf3
commit ffe0a3e7c8
4 changed files with 69 additions and 0 deletions

15
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,15 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}

View File

@ -1,2 +1,3 @@
# gologger
a logger used in go

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module gologger
go 1.19

50
main.go Normal file
View File

@ -0,0 +1,50 @@
package main
import (
"fmt"
"time"
)
const (
NoLog = iota
Error
Warn
Info
Debug
Trace
)
type Logger struct {
name string
level int
}
func (l *Logger) log(level int, msg string) {
now := time.Now()
if level <= l.level {
fmt.Println(now.Format("2006-01-02 15:04:05"), " ", l.name, ": ", msg)
}
}
func (l *Logger) Error(msg string) {
l.log(Error, msg)
}
func (l *Logger) Warn(msg string) {
l.log(Warn, msg)
}
func (l *Logger) Info(msg string) {
l.log(Info, msg)
}
func (l *Logger) Debug(msg string) {
l.log(Debug, msg)
}
func (l *Logger) Trace(msg string) {
l.log(Trace, msg)
}
func GetLogger(name string) Logger {
return Logger{name, 0}
}