diff --git a/README.md b/README.md index 1d46b78..b8dcefa 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,6 @@ You can build the example program by running the following command in the projec ``` $ make build - ``` ### Running @@ -42,7 +41,6 @@ You can run the example program by running the following command in the project ``` $ ./bin/main - ``` ## Usage @@ -51,7 +49,6 @@ Import the framework as follows: ``` import "github.com/kubecub/log" - ``` ### Code Examples @@ -76,7 +73,6 @@ func main() { "size": 10, }).Info("A group of walrus emerges from the ocean") } - ``` ## Contributing diff --git a/context.go b/context.go index 0a4a963..088f6d6 100644 --- a/context.go +++ b/context.go @@ -1,3 +1,8 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + package log import ( diff --git a/distribution/doc.go b/distribution/doc.go index 9a04f24..41f0443 100644 --- a/distribution/doc.go +++ b/distribution/doc.go @@ -1 +1,6 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + package distribution // import "github.com/kubecub/log/distribution" diff --git a/distribution/logger.go b/distribution/logger.go index 2522f50..f912bbb 100644 --- a/distribution/logger.go +++ b/distribution/logger.go @@ -1,3 +1,8 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + // Package distribution implements a logger which compatible to logrus/std log/prometheus. package distribution diff --git a/encoder.go b/encoder.go index 4e3ab10..8422552 100644 --- a/encoder.go +++ b/encoder.go @@ -1,3 +1,8 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + package log import ( diff --git a/example/context/doc.go b/example/context/doc.go new file mode 100644 index 0000000..d7f93e9 --- /dev/null +++ b/example/context/doc.go @@ -0,0 +1,6 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + +package main //github.com/kubecub/log/example diff --git a/example/context/main.go b/example/context/main.go new file mode 100644 index 0000000..3ff90e9 --- /dev/null +++ b/example/context/main.go @@ -0,0 +1,60 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + +package main + +import ( + "context" + "flag" + + "github.com/kubecub/log" +) + +var ( + h bool + + level int + format string +) + +func main() { + flag.BoolVar(&h, "h", false, "Print this help.") + flag.IntVar(&level, "l", 0, "Log level.") + flag.StringVar(&format, "f", "console", "log output format.") + + flag.Parse() + + if h { + flag.Usage() + + return + } + + // logger配置 + opts := &log.Options{ + Level: "debug", + Format: "console", + EnableColor: true, + DisableCaller: true, + OutputPaths: []string{"test.log", "stdout"}, + ErrorOutputPaths: []string{"error.log"}, + } + // 初始化全局logger + log.Init(opts) + defer log.Flush() + + // WithValues使用 + lv := log.WithValues("X-Request-ID", "7a7b9f24-4cae-4b2a-9464-69088b45b904") + + // Context使用 + lv.Infof("Start to call pirntString function") + ctx := lv.WithContext(context.Background()) + pirntString(ctx, "World") +} + +func pirntString(ctx context.Context, str string) { + lc := log.FromContext(ctx) + lc.Infof("Hello %s", str) +} diff --git a/example/example.go b/example/example.go new file mode 100644 index 0000000..645db3c --- /dev/null +++ b/example/example.go @@ -0,0 +1,71 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + +package main + +import ( + "context" + "flag" + + "github.com/kubecub/log" +) + +var ( + h bool + + level int + format string +) + +func main() { + flag.BoolVar(&h, "h", false, "Print this help.") + flag.IntVar(&level, "l", 0, "Log level.") + flag.StringVar(&format, "f", "console", "log output format.") + + flag.Parse() + + if h { + flag.Usage() + + return + } + + // logger配置 + opts := &log.Options{ + Level: "debug", + Format: "console", + EnableColor: true, // if you need output to local path, with EnableColor must be false. + DisableCaller: true, + OutputPaths: []string{"test.log", "stdout"}, + ErrorOutputPaths: []string{"error.log"}, + } + // 初始化全局logger + log.Init(opts) + defer log.Flush() + + // Debug、Info(with field)、Warnf、Errorw使用 + log.Debug("This is a debug message") + log.Info("This is a info message", log.Int32("int_key", 10)) + log.Warnf("This is a formatted %s message", "warn") + log.Errorw("Message printed with Errorw", "X-Request-ID", "fbf54504-64da-4088-9b86-67824a7fb508") + + // WithValues使用 + lv := log.WithValues("X-Request-ID", "7a7b9f24-4cae-4b2a-9464-69088b45b904") + lv.Infow("Info message printed with [WithValues] logger") + lv.Infow("Debug message printed with [WithValues] logger") + + // Context使用 + ctx := lv.WithContext(context.Background()) + lc := log.FromContext(ctx) + lc.Info("Message printed with [WithContext] logger") + + ln := lv.WithName("test") + ln.Info("Message printed with [WithName] logger") + + // V level使用 + log.V(log.InfoLevel).Info("This is a V level message") + log.V(log.ErrorLevel). + Infow("This is a V level message with fields", "X-Request-ID", "7a7b9f24-4cae-4b2a-9464-69088b45b904") +} diff --git a/example/simple/doc.go b/example/simple/doc.go new file mode 100644 index 0000000..fad86f1 --- /dev/null +++ b/example/simple/doc.go @@ -0,0 +1,6 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + +package main //github.com/kubecub/log/simple \ No newline at end of file diff --git a/example/simple/simple.go b/example/simple/simple.go new file mode 100644 index 0000000..93b9022 --- /dev/null +++ b/example/simple/simple.go @@ -0,0 +1,12 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + +package main + +import "github.com/kubecub/log" + +func main() { + log.Infof("this is a test log, message: %s", "good") +} diff --git a/example/vlevel /doc.go b/example/vlevel /doc.go new file mode 100644 index 0000000..7835bc3 --- /dev/null +++ b/example/vlevel /doc.go @@ -0,0 +1,6 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + +package main //github.com/kubecub/log \ No newline at end of file diff --git a/example/vlevel /v_level.go b/example/vlevel /v_level.go new file mode 100644 index 0000000..bbe16f4 --- /dev/null +++ b/example/vlevel /v_level.go @@ -0,0 +1,17 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + +package main + +import ( + "github.com/kubecub/log" +) + +func main() { + defer log.Flush() + + log.V(0).Info("This is a V level message") + log.V(0).Infow("This is a V level message with fields", "X-Request-ID", "7a7b9f24-4cae-4b2a-9464-69088b45b904") +} diff --git a/json/json.go b/json/json.go index 226e371..51cedb3 100644 --- a/json/json.go +++ b/json/json.go @@ -1,3 +1,8 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + // //go:build !jsoniter // // +build !jsoniter diff --git a/json/jsoniter.go b/json/jsoniter.go index 1b9ff67..21ab7bd 100644 --- a/json/jsoniter.go +++ b/json/jsoniter.go @@ -1,3 +1,8 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + package json import jsoniter "github.com/json-iterator/go" diff --git a/klog/logger.go b/klog/logger.go index 665e287..b9cf3ed 100644 --- a/klog/logger.go +++ b/klog/logger.go @@ -1,3 +1,8 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + // Package klog init klog logger. klog is used by kubernetes, this can compatible the kubernetes packages. package klog diff --git a/log.go b/log.go index 1c2f8da..d468fb9 100644 --- a/log.go +++ b/log.go @@ -1,3 +1,8 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + package log import ( diff --git a/logrus/hook.go b/logrus/hook.go index ef1b25f..45d97e9 100644 --- a/logrus/hook.go +++ b/logrus/hook.go @@ -1,3 +1,8 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + package logrus import ( diff --git a/logrus/logger.go b/logrus/logger.go index da12685..ab43cd4 100644 --- a/logrus/logger.go +++ b/logrus/logger.go @@ -1,3 +1,8 @@ +// Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. + // Package logrus adds a hook to the logrus logger hooks. package logrus