How to use init method of example_test Package

Best Got code snippet using example_test.init

example_test.go

Source:example_test.go Github

copy

Full Screen

1package zl_test2import (3 "fmt"4 "log"5 "os"6 "testing"7 "github.com/nkmr-jp/zl"8 "go.uber.org/zap"9)10var (11 version string // version git revision or tag. set from go cli.12 srcRootDir string // srcRootDir set from cli.13)14func TestMain(m *testing.M) {15 if err := os.RemoveAll("./log"); err != nil {16 log.Fatal(err)17 }18 m.Run()19}20func Example() {21 // Set options22 fileName := "./log/example.jsonl"23 zl.SetLevel(zl.DebugLevel)24 // zl.SetOutput(zl.PrettyOutput)25 zl.SetOmitKeys(zl.TimeKey, zl.CallerKey, zl.VersionKey, zl.HostnameKey, zl.StacktraceKey, zl.PIDKey)26 zl.SetRotateFileName(fileName)27 // Initialize28 zl.Init()29 defer zl.Sync() // flush log buffer30 // Write logs31 console := "display to console when output type is pretty"32 zl.Info("USER_INFO", zap.String("user_name", "Alice"), zap.Int("user_age", 20)) // can use zap fields.33 err := fmt.Errorf("error message")34 zl.Error("ERROR_MESSAGE", err) // error level log must with error message.35 zl.Debug("DEBUG_MESSAGE")36 zl.Warn("WARN_MESSAGE", zap.Error(err)) // warn level log with error message.37 zl.WarnErr("WARN_MESSAGE_WITH_ERROR", err) // same to above.38 zl.Info("DISPLAY_TO_CONSOLE", zl.Console(console))39 zl.Info("DISPLAY_TO_CONSOLE", zl.Consolep(nil))40 zl.DebugErr("DEBUG_MESSAGE_WITH_ERROR_AND_CONSOLE", err, zl.Consolep(&console))41 bytes, _ := os.ReadFile(fileName)42 fmt.Println(string(bytes))43 // Output to stderr with colored:44 // zl.go:44: DEBUG INIT_LOGGER: Severity: DEBUG, Output: Pretty, FileName: ./log/example.jsonl45 // example_test.go:39: INFO USER_INFO46 // example_test.go:41: ERROR ERROR_MESSAGE: error message47 // example_test.go:42: DEBUG DEBUG_MESSAGE48 // example_test.go:43: WARN WARN_MESSAGE49 // example_test.go:44: WARN WARN_MESSAGE_WITH_ERROR: error message50 // example_test.go:45: INFO DISPLAY_TO_CONSOLE: display to console when output type is pretty51 // example_test.go:46: DEBUG DEBUG_MESSAGE_WITH_ERROR_AND_CONSOLE: error message , display to console when output type is pretty52 // zl.go:131: DEBUG FLUSH_LOG_BUFFER53 // Output:54 // {"severity":"DEBUG","function":"github.com/nkmr-jp/zl.Init.func1","message":"INIT_LOGGER","console":"Severity: DEBUG, Output: Pretty, File: ./log/example.jsonl"}55 // {"severity":"INFO","function":"github.com/nkmr-jp/zl_test.Example","message":"USER_INFO","user_name":"Alice","user_age":20}56 // {"severity":"ERROR","function":"github.com/nkmr-jp/zl_test.Example","message":"ERROR_MESSAGE","error":"error message"}57 // {"severity":"DEBUG","function":"github.com/nkmr-jp/zl_test.Example","message":"DEBUG_MESSAGE"}58 // {"severity":"WARN","function":"github.com/nkmr-jp/zl_test.Example","message":"WARN_MESSAGE","error":"error message"}59 // {"severity":"WARN","function":"github.com/nkmr-jp/zl_test.Example","message":"WARN_MESSAGE_WITH_ERROR","error":"error message"}60 // {"severity":"INFO","function":"github.com/nkmr-jp/zl_test.Example","message":"DISPLAY_TO_CONSOLE","console":"display to console when output type is pretty"}61 // {"severity":"INFO","function":"github.com/nkmr-jp/zl_test.Example","message":"DISPLAY_TO_CONSOLE","console":null}62 // {"severity":"DEBUG","function":"github.com/nkmr-jp/zl_test.Example","message":"DEBUG_MESSAGE_WITH_ERROR_AND_CONSOLE","console":"display to console when output type is pretty","error":"error message"}63}64func ExampleSetVersion() {65 zl.Cleanup() // removes logger and resets settings.66 urlFormat := "https://github.com/nkmr-jp/zl/blob/%s"67 // Actually, it is recommended to pass the value from the command line of go.68 // ex. `go run -ldflags "-X main.version=v1.0.0 -X main.srcRootDir=$PWD" main.go`.69 version = "v1.0.0"70 srcRootDir, _ = os.Getwd()71 // Set Options72 zl.SetLevel(zl.DebugLevel)73 zl.SetVersion(version)74 fileName := fmt.Sprintf("./log/example-set-version_%s.jsonl", zl.GetVersion())75 zl.SetRotateFileName(fileName)76 zl.SetRepositoryCallerEncoder(urlFormat, version, srcRootDir)77 zl.SetOmitKeys(zl.TimeKey, zl.FunctionKey, zl.HostnameKey, zl.PIDKey)78 zl.SetOutput(zl.ConsoleAndFileOutput)79 // Initialize80 zl.Init()81 defer zl.Sync() // flush log buffer82 // Write logs83 zl.Info("INFO_MESSAGE", zap.String("detail", "detail info xxxxxxxxxxxxxxxxx"))84 zl.Warn("WARN_MESSAGE", zap.String("detail", "detail info xxxxxxxxxxxxxxxxx"))85 bytes, _ := os.ReadFile(fileName)86 fmt.Println(string(bytes))87 // Output:88 // {"severity":"DEBUG","caller":"zl/zl.go:69","message":"INIT_LOGGER","version":"v1.0.0","console":"Severity: DEBUG, Output: ConsoleAndFile, File: ./log/example-set-version_v1.0.0.jsonl"}89 // {"severity":"INFO","caller":"https://github.com/nkmr-jp/zl/blob/v1.0.0/example_test.go#L99","message":"INFO_MESSAGE","version":"v1.0.0","detail":"detail info xxxxxxxxxxxxxxxxx"}90 // {"severity":"WARN","caller":"https://github.com/nkmr-jp/zl/blob/v1.0.0/example_test.go#L100","message":"WARN_MESSAGE","version":"v1.0.0","detail":"detail info xxxxxxxxxxxxxxxxx"}91}92func ExampleNew() {93 zl.Cleanup() // removes logger and resets settings.94 // Set options95 traceIDField := "trace"96 fileName := "./log/example-new.jsonl"97 zl.SetConsoleFields(traceIDField)98 zl.SetLevel(zl.DebugLevel)99 zl.SetOmitKeys(zl.TimeKey, zl.CallerKey, zl.FunctionKey, zl.VersionKey, zl.HostnameKey, zl.StacktraceKey, zl.PIDKey)100 zl.SetOutput(zl.PrettyOutput)101 zl.SetRotateFileName(fileName)102 traceID := "c7mg6hnr2g4l6vvuao50" // xid.New().String()103 // Initialize104 zl.Init()105 defer zl.Sync() // flush log buffer106 // New107 // e.g. Use this when you want to add a common value in the scope of a context, such as an API request.108 l1 := zl.New(109 zap.Int("user_id", 1),110 zap.String(traceIDField, traceID),111 ).Named("log1")112 l2 := zl.New(113 zap.Int("user_id", 1),114 zap.String(traceIDField, traceID),115 ).Named("log2")116 // Write logs117 err := fmt.Errorf("error")118 zl.Info("GLOBAL_INFO")119 l1.Info("CONTEXT_SCOPE_INFO", zl.Consolef("some message to console: %s", "test"))120 l1.Error("CONTEXT_SCOPE_ERROR", fmt.Errorf("context scope error message"))121 l2.Info("CONTEXT_SCOPE_INFO2", zl.Consolef("some message to console: %s", "test"))122 l2.Debug("TEST")123 l2.Warn("TEST")124 l2.Error("TEST", err)125 l2.InfoErr("TEST", err)126 l2.DebugErr("TEST", err)127 l2.WarnErr("TEST", err)128 bytes, _ := os.ReadFile(fileName)129 fmt.Println(string(bytes))130 // Output to stderr with colored:131 // zl.go:69: DEBUG INIT_LOGGER : Severity: DEBUG, Output: Pretty, File: ./log/example-new.jsonl132 // example_test.go:142: INFO GLOBAL_INFO133 // log1 | example_test.go:143: INFO CONTEXT_SCOPE_INFO : some message to console: test : c7mg6hnr2g4l6vvuao50134 // log1 | example_test.go:144: ERROR CONTEXT_SCOPE_ERROR : context scope error message : c7mg6hnr2g4l6vvuao50135 // log2 | example_test.go:145: INFO CONTEXT_SCOPE_INFO2 : some message to console: test : c7mg6hnr2g4l6vvuao50136 // log2 | example_test.go:146: DEBUG TEST : c7mg6hnr2g4l6vvuao50137 // log2 | example_test.go:147: WARN TEST : c7mg6hnr2g4l6vvuao50138 // log2 | example_test.go:148: ERROR TEST : error : c7mg6hnr2g4l6vvuao50139 // log2 | example_test.go:149: INFO TEST : error : c7mg6hnr2g4l6vvuao50140 // log2 | example_test.go:150: DEBUG TEST : error : c7mg6hnr2g4l6vvuao50141 // log2 | example_test.go:151: WARN TEST : error : c7mg6hnr2g4l6vvuao50142 // Output:143 // {"severity":"DEBUG","message":"INIT_LOGGER","console":"Severity: DEBUG, Output: Pretty, File: ./log/example-new.jsonl"}144 // {"severity":"INFO","message":"GLOBAL_INFO"}145 // {"severity":"INFO","logger":"log1","message":"CONTEXT_SCOPE_INFO","console":"some message to console: test","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}146 // {"severity":"ERROR","logger":"log1","message":"CONTEXT_SCOPE_ERROR","error":"context scope error message","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}147 // {"severity":"INFO","logger":"log2","message":"CONTEXT_SCOPE_INFO2","console":"some message to console: test","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}148 // {"severity":"DEBUG","logger":"log2","message":"TEST","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}149 // {"severity":"WARN","logger":"log2","message":"TEST","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}150 // {"severity":"ERROR","logger":"log2","message":"TEST","error":"error","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}151 // {"severity":"INFO","logger":"log2","message":"TEST","error":"error","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}152 // {"severity":"DEBUG","logger":"log2","message":"TEST","error":"error","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}153 // {"severity":"WARN","logger":"log2","message":"TEST","error":"error","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}154}155func ExampleSetLevelByString() {156 zl.Cleanup() // removes logger and resets settings.157 zl.SetLevelByString("DEBUG")158 zl.SetOutputByString("Console")159 zl.SetStdout()160 zl.SetOmitKeys(zl.TimeKey, zl.CallerKey, zl.FunctionKey, zl.VersionKey, zl.HostnameKey, zl.StacktraceKey, zl.PIDKey)161 zl.Init()162 zl.Debug("DEBUG_MESSAGE")163 zl.Info("INFO_MESSAGE")164 // Output:165 // {"severity":"DEBUG","message":"INIT_LOGGER","console":"Severity: DEBUG, Output: Console"}166 // {"severity":"DEBUG","message":"DEBUG_MESSAGE"}167 // {"severity":"INFO","message":"INFO_MESSAGE"}168}169func ExampleError() {170 zl.Cleanup() // removes logger and resets settings.171 zl.SetOmitKeys(zl.TimeKey, zl.VersionKey, zl.HostnameKey)172 // Initialize173 zl.Init()174 defer zl.Sync() // flush log buffer175 zl.Error("ERROR_WITH_STACKTRACE", fmt.Errorf("error occurred"))176 zl.Info("INFO")177 zl.InfoErr("INFO_ERR", fmt.Errorf("error"))178 zl.Error("ERROR_WITH_STACKTRACE", fmt.Errorf("error occurred"))179 // Output:180}181func ExampleSetRotateMaxSize() {182 zl.Cleanup() // removes logger and resets settings.183 zl.SetLevel(zl.DebugLevel)184 zl.SetOutput(zl.FileOutput)185 zl.SetRotateFileName("./log/example-SetRotateMaxSize.jsonl")186 zl.SetRotateMaxSize(100)187 zl.SetRotateMaxAge(7)188 zl.SetRotateMaxBackups(3)189 zl.SetRotateLocalTime(true)190 zl.SetRotateCompress(true)191 // Initialize192 zl.Init()193 defer zl.Sync() // flush log buffer194 // Output:195}196func ExampleDump() {197 zl.Cleanup() // removes logger and resets settings.198 zl.SetLevel(zl.DebugLevel)199 zl.SetRotateFileName("./log/example-Dump.jsonl")200 zl.Init()201 defer zl.Sync() // flush log buffer202 zl.Dump("test")203 // Output:204}205func ExampleSyncWhenStop() {206 zl.Cleanup() // removes logger and resets settings.207 zl.SetLevel(zl.DebugLevel)208 zl.SetRotateFileName("./log/example-Dump.jsonl")209 zl.Init()210 defer zl.Sync()211 zl.SyncWhenStop()212 zl.Info("TEST")213 // Output:214}215func ExampleOthers() {216 zl.Cleanup()217 zl.SetLevel(zl.DebugLevel)218 zl.SetOmitKeys(zl.MessageKey, zl.LevelKey, zl.LoggerKey)219 zl.GetVersion()220 zl.SetOutputByString("")221 zl.SetRepositoryCallerEncoder("", "", "")222 zl.SetSeparator(" --- ")223 zl.SetOutput(zl.FileOutput)224 zl.SetRotateFileName("./log/example-Dump.jsonl")225 zl.Init()226 defer zl.Sync()227 zl.InfoErr("TEST", fmt.Errorf("error"))228 l1 := zl.New(229 zap.Int("user_id", 1),230 ).Named("log1")231 l1.Info("test")232 // Output:233}...

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(greetings.Hello("Gladys"))4}5import (6func main() {7 fmt.Println(greetings.Hello("Gladys"))8}9import (10func main() {11 fmt.Println(greetings.Hello("Gladys"))12}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1func TestMain(m *testing.M) {2 fmt.Println("TestMain")3 os.Exit(m.Run())4}5func TestExampleTest(t *testing.T) {6 fmt.Println("TestExampleTest")7}8func TestExampleTest2(t *testing.T) {9 fmt.Println("TestExampleTest2")10}11func TestExampleTest3(t *testing.T) {12 fmt.Println("TestExampleTest3")13}14func TestExampleTest4(t *testing.T) {15 fmt.Println("TestExampleTest4")16}17func TestExampleTest5(t *testing.T) {18 fmt.Println("TestExampleTest5")19}20func TestExampleTest6(t *testing.T) {21 fmt.Println("TestExampleTest6")22}23func TestExampleTest7(t *testing.T) {24 fmt.Println("TestExampleTest7")25}26func TestExampleTest8(t *testing.T) {27 fmt.Println("TestExampleTest8")28}29func TestExampleTest9(t *testing.T) {30 fmt.Println("TestExampleTest9")31}32func TestExampleTest10(t *testing.T) {33 fmt.Println("TestExampleTest10")34}35func TestExampleTest11(t *testing.T) {36 fmt.Println("TestExampleTest11")37}38func TestExampleTest12(t *testing.T) {39 fmt.Println("TestExampleTest12")40}41func TestExampleTest13(t *testing.T) {42 fmt.Println("TestExampleTest13")43}44func TestExampleTest14(t *testing.T) {45 fmt.Println("TestExampleTest14")46}47func TestExampleTest15(t *testing.T) {48 fmt.Println("TestExampleTest15")49}50func TestExampleTest16(t *testing.T) {51 fmt.Println("TestExampleTest16")52}53func TestExampleTest17(t *testing.T) {54 fmt.Println("TestExampleTest17")55}56func TestExampleTest18(t *testing.T) {57 fmt.Println("TestExampleTest18")58}59func TestExampleTest19(t *testing.T) {60 fmt.Println("TestExampleTest19")61}62func TestExampleTest20(t *testing.T) {63 fmt.Println("TestExampleTest20")64}65func TestExampleTest21(t *testing.T) {66 fmt.Println("TestExampleTest21")67}68func TestExampleTest22(t *testing.T) {69 fmt.Println("TestExampleTest22")70}71func TestExampleTest23(t *testing.T) {72 fmt.Println("TestExampleTest23")73}74func TestExampleTest24(t *testing.T) {75 fmt.Println("TestExampleTest24")

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Got automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful