This repository has been archived by the owner on Sep 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
impl_test.go
79 lines (69 loc) · 1.62 KB
/
impl_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Copyright (c) 2020-present unTill Pro, Ltd. and Contributors
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package logger
import (
"context"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_BasicUsage(t *testing.T) {
ctx := context.Background()
// "Hello world"
{
// Use context.Background() if you do not have a context
Error(context.Background(), "Hello world", "arg1", "arg2")
Warning(ctx, "My warning")
Info(ctx, "My info")
// IsDebug() is used to avoid unnecessary calculations
if IsDebug(ctx) {
Debug(ctx, "!!! You should NOT see it since default level is INFO")
}
}
// Changing LogLevel
{
SetLogLevel(LogLevelDebug)
if IsDebug(ctx) {
Debug(ctx, "Now you should see my Debug")
}
SetLogLevel(LogLevelError)
Debug(ctx, "!!! You should NOT see my Debug")
Warning(ctx, "!!! You should NOT see my warning")
SetLogLevel(LogLevelInfo)
Warning(ctx, "You should see my warning")
Warning(ctx, "You should see my info")
}
// From struct
{
m := mystruct{}
m.logMe()
}
}
type mystruct struct {
}
func (m *mystruct) logMe() {
Error(context.Background(), "OOPS")
}
func Benchmark_FuncForPC(b *testing.B) {
var funcName string
for i := 0; i < b.N; i++ {
pc, _, _, ok := runtime.Caller(2)
details := runtime.FuncForPC(pc)
if ok && details != nil {
elems := strings.Split(details.Name(), "/")
if len(elems) > 1 {
funcName = elems[len(elems)-1]
} else {
funcName = details.Name()
}
} else {
funcName = ""
}
}
assert.True(b, len(funcName) > 0)
}