Skip to content

Commit

Permalink
Merge pull request #2 from CloudOS-Group3/feature/log
Browse files Browse the repository at this point in the history
feat: make a log util for info, debug, warn, error, fatal 5 levels
  • Loading branch information
lulu010722 authored Apr 20, 2024
2 parents be49744 + aabb0d2 commit 1151367
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ module minik8s
go 1.22

require (
github.com/fatih/color v1.16.0
github.com/spf13/cobra v1.8.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.14.0 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
77 changes: 77 additions & 0 deletions util/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package log

import (
"fmt"
"os"
"runtime"
"time"

"github.com/fatih/color"
)

var (
white = color.New(color.FgWhite).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
blue = color.New(color.FgBlue).SprintFunc()
yellow = color.New(color.FgYellow).SprintFunc()
red = color.New(color.FgRed).SprintFunc()
)

func Info(format string, args ...interface{}) {

t := time.Now().Format("2006-01-02 15:04:05")
pc, file, line, _ := runtime.Caller(1)

prefix := fmt.Sprintf("%s %s:%s:%d:", t, file, runtime.FuncForPC(pc).Name(), line)
content := fmt.Sprintf(format, args...)

fmt.Println(blue(prefix), white(content))
}

func Debug(format string, args ...interface{}) {

t := time.Now().Format("2006-01-02 15:04:05")
pc, file, line, _ := runtime.Caller(1)

prefix := fmt.Sprintf("%s %s:%s:%d:", t, file, runtime.FuncForPC(pc).Name(), line)
content := fmt.Sprintf(format, args...)

fmt.Println(green(prefix), white(content))
}

func Warn(format string, args ...interface{}) {

t := time.Now().Format("2006-01-02 15:04:05")
pc, file, line, _ := runtime.Caller(1)

prefix := fmt.Sprintf("%s %s:%s:%d:", t, file, runtime.FuncForPC(pc).Name(), line)
content := fmt.Sprintf(format, args...)

fmt.Println(yellow(prefix), white(content))
}

func Error(format string, args ...interface{}) {

t := time.Now().Format("2006-01-02 15:04:05")
pc, file, line, _ := runtime.Caller(1)

prefix := fmt.Sprintf("%s %s:%s:%d:", t, file, runtime.FuncForPC(pc).Name(), line)
content := fmt.Sprintf(format, args...)

fmt.Println(red(prefix), white(content))
}

func Fatal(format string, args ...interface{}) {

t := time.Now().Format("2006-01-02 15:04:05")
pc, file, line, _ := runtime.Caller(1)

prefix := fmt.Sprintf("%s %s:%s:%d:", t, file, runtime.FuncForPC(pc).Name(), line)
content := fmt.Sprintf(format, args...)

fmt.Println(red(prefix), white(content))

// will only exit in this level because it is fatal
os.Exit(1)
}

0 comments on commit 1151367

Please sign in to comment.