From ffe0a3e7c8fae35ed1ec7014fac977fdd713d8d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B9=BF?= Date: Mon, 23 Jan 2023 09:59:30 +0800 Subject: [PATCH] add code --- .vscode/launch.json | 15 ++++++++++++++ README.md | 1 + go.mod | 3 +++ main.go | 50 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 go.mod create mode 100644 main.go diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..90dc044 --- /dev/null +++ b/.vscode/launch.json @@ -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}" + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 71d08ea..6833230 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # gologger +a logger used in go \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8552da5 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gologger + +go 1.19 diff --git a/main.go b/main.go new file mode 100644 index 0000000..8d47081 --- /dev/null +++ b/main.go @@ -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} +}