Skip to content

Commit

Permalink
feat: main test example
Browse files Browse the repository at this point in the history
Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com>
  • Loading branch information
cubxxw committed Apr 24, 2023
1 parent d16d43d commit 22ed1ec
Show file tree
Hide file tree
Showing 18 changed files with 228 additions and 4 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ You can build the example program by running the following command in the projec

```
$ make build
```

### Running
Expand All @@ -42,7 +41,6 @@ You can run the example program by running the following command in the project

```
$ ./bin/main
```

## Usage
Expand All @@ -51,7 +49,6 @@ Import the framework as follows:

```
import "github.com/kubecub/log"
```

### Code Examples
Expand All @@ -76,7 +73,6 @@ func main() {
"size": 10,
}).Info("A group of walrus emerges from the ocean")
}
```

## Contributing
Expand Down
5 changes: 5 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
5 changes: 5 additions & 0 deletions distribution/doc.go
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 5 additions & 0 deletions distribution/logger.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 5 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
6 changes: 6 additions & 0 deletions example/context/doc.go
Original file line number Diff line number Diff line change
@@ -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
60 changes: 60 additions & 0 deletions example/context/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
71 changes: 71 additions & 0 deletions example/example.go
Original file line number Diff line number Diff line change
@@ -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")
}
6 changes: 6 additions & 0 deletions example/simple/doc.go
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions example/simple/simple.go
Original file line number Diff line number Diff line change
@@ -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")
}
6 changes: 6 additions & 0 deletions example/vlevel /doc.go
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions example/vlevel /v_level.go
Original file line number Diff line number Diff line change
@@ -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")
}
5 changes: 5 additions & 0 deletions json/json.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 5 additions & 0 deletions json/jsoniter.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
5 changes: 5 additions & 0 deletions klog/logger.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 5 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
5 changes: 5 additions & 0 deletions logrus/hook.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
5 changes: 5 additions & 0 deletions logrus/logger.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 22ed1ec

Please sign in to comment.