You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							59 lines
						
					
					
						
							1.2 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							59 lines
						
					
					
						
							1.2 KiB
						
					
					
				
								// Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
							 | 
						|
								// Use of this source code is governed by a MIT style
							 | 
						|
								// license that can be found in the LICENSE file.
							 | 
						|
								
							 | 
						|
								package main
							 | 
						|
								
							 | 
						|
								import (
							 | 
						|
									"context"
							 | 
						|
									"flag"
							 | 
						|
								
							 | 
						|
									"cynking.com/pkg/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)
							 | 
						|
								}
							 |