11/*
2- * Copyright 2022-2024 Thorsten A. Knieling
2+ * Copyright 2022-2026 Thorsten A. Knieling
33*
44* Licensed under the Apache License, Version 2.0 (the "License");
55* you may not use this file except in compliance with the License.
1212package log
1313
1414import (
15- "encoding/json"
1615 "fmt"
1716 "io"
1817 "net/url"
1918 "os"
20- "path/filepath"
21- "runtime"
2219 "testing"
2320 "time"
2421
@@ -29,8 +26,8 @@ import (
2926)
3027
3128func initTestLogWithFile (t * testing.T , fileName string ) error {
32- err := initLogLevelWithFile (fileName , zapcore .DebugLevel )
33- if err != nil {
29+ err := InitZapLogLevelWithFile (fileName , zapcore .DebugLevel )
30+ if ! assert . NoError ( t , err ) {
3431 t .Fatalf ("error opening file: %v" , err )
3532 }
3633 return err
@@ -41,59 +38,16 @@ func newWinFileSink(u *url.URL) (zap.Sink, error) {
4138 return os .OpenFile (u .Path [1 :], os .O_WRONLY | os .O_APPEND | os .O_CREATE , 0644 )
4239}
4340
44- func initLogLevelWithFile (fileName string , level zapcore.Level ) (err error ) {
45- p := os .Getenv ("LOGPATH" )
46- if p == "" {
47- p = os .TempDir ()
48- }
49- var name string
50- if runtime .GOOS == "windows" {
51- zap .RegisterSink ("winfile" , newWinFileSink )
52- // OutputPaths: []string{"stdout", "winfile:///" + filepath.Join(GlobalConfigDir.Path, "info.log.json")},
53- name = "winfile:///" + p + string (os .PathSeparator ) + fileName
54- } else {
55- name = "file://" + filepath .ToSlash (p + string (os .PathSeparator )+ fileName )
56- }
57-
58- rawJSON := []byte (`{
59- "level": "error",
60- "encoding": "console",
61- "outputPaths": [ "XXX"],
62- "errorOutputPaths": ["stderr"],
63- "encoderConfig": {
64- "messageKey": "message",
65- "levelKey": "level",
66- "levelEncoder": "lowercase"
67- }
68- }` )
69-
70- var cfg zap.Config
71- if err := json .Unmarshal (rawJSON , & cfg ); err != nil {
72- return err
73- }
74- cfg .Level .SetLevel (level )
75- cfg .OutputPaths = []string {name }
76- logger , err := cfg .Build ()
77- if err != nil {
78- return err
79- }
80- defer logger .Sync ()
81-
82- sugar := logger .Sugar ()
83- InitLog (sugar )
84-
85- sugar .Infof ("AdabasGoApi logger initialization succeeded" )
86- return nil
87- }
88-
8941func doTrackCall () {
9042 defer TimeTrack (time .Now (), "Time Track Unit test " )
9143
9244}
9345
9446func TestLogZap (t * testing.T ) {
9547 fileName := "zap.log"
96- os .Remove (os .TempDir () + "/" + fileName )
48+ fileName = os .TempDir () + "/" + fileName
49+ fmt .Println ("Use log file:" , fileName )
50+ os .Remove (fileName )
9751 err := initTestLogWithFile (t , fileName )
9852 if ! assert .NoError (t , err ) {
9953 fmt .Println (err )
@@ -111,20 +65,21 @@ func TestLogZap(t *testing.T) {
11165 SetDebugLevel (false )
11266 SetDebugLevel (d )
11367
114- flog , err := os .Open (os . TempDir () + "/" + fileName )
68+ flog , err := os .Open (fileName )
11569 if ! assert .NoError (t , err ) {
11670 return
11771 }
11872 logInfo , err := io .ReadAll (flog )
11973 if ! assert .NoError (t , err ) {
12074 return
12175 }
122- assert .Regexp (t , `info AdabasGoApi logger initialization succeeded
123- info Time Track Unit test took \d*ns
124- debug This is a test of data HELLO
125- debug ABC
126- debug XXXX
127- debug` , string (logInfo ))
76+ assert .Regexp (t , `....-..-.. ..:..:..\tinfo\tStart logging with level debug
77+ ....-..-.. ..:..:..\tinfo\tTime Track Unit test took [0-8]*ns
78+ ....-..-.. ..:..:..\tdebug\tThis is a test of data HELLO
79+ ....-..-.. ..:..:..\tdebug\tABC
80+ ....-..-.. ..:..:..\tdebug\tXXXX
81+ ....-..-.. ..:..:..\tdebug\t
82+ ` , string (logInfo ))
12883}
12984
13085func TestLogrus (t * testing.T ) {
@@ -208,3 +163,32 @@ func TestCache(t *testing.T) {
208163 Log .Errorf ("ERROR: Post-log information" )
209164 assert .Equal (t , testResult , testLog .testLog )
210165}
166+
167+ func TestStackTest (t * testing.T ) {
168+ InitZapLogLevelWithFile ("StackTest.log" , zapcore .DebugLevel )
169+ Log .Debugf ("DEBUG: Post-log information" )
170+ Log .Infof ("INFO: Post-log information" )
171+ Log .Errorf ("ERROR: Post-log information" )
172+ stack1 ()
173+ }
174+
175+ func stack1 () {
176+ LogFunctionStart ()
177+ stack2 ()
178+ defer LogFunctionEnd (time .Now ())
179+
180+ }
181+
182+ func stack2 () {
183+ LogFunctionStart ()
184+ stack3 ()
185+ defer LogFunctionEnd (time .Now ())
186+
187+ }
188+
189+ func stack3 () {
190+ LogFunctionStart ()
191+ Log .Debugf ("In stack3" )
192+ defer LogFunctionEnd (time .Now ())
193+
194+ }
0 commit comments