// OutputPaths is a list of URLs or file paths to write logging output to. // See Open for details. OutputPaths []string`json:"outputPaths" yaml:"outputPaths"`
// Open is a high-level wrapper that takes a variadic number of URLs, opens or // creates each of the specified resources, and combines them into a locked // WriteSyncer. It also returns any error encountered and a function to close // any opened files. // // Passing no URLs returns a no-op WriteSyncer. Zap handles URLs without a // scheme and URLs with the "file" scheme. Third-party code may register // factories for other schemes using RegisterSink. // // URLs with the "file" scheme must use absolute paths on the local // filesystem. No user, password, port, fragments, or query parameters are // allowed, and the hostname must be empty or "localhost". // // Since it's common to write logs to the local filesystem, URLs without a // scheme (e.g., "/var/log/foo.log") are treated as local file paths. Without // a scheme, the special paths "stdout" and "stderr" are interpreted as // os.Stdout and os.Stderr. When specified without a scheme, relative file // paths also work. funcOpen(paths ...string)(zapcore.WriteSyncer, func(), error) { writers, close, err := open(paths) if err != nil { returnnil, nil, err }
// RegisterSink registers a user-supplied factory for all sinks with a // particular scheme. // // All schemes must be ASCII, valid under section 3.1 of RFC 3986 // (https://tools.ietf.org/html/rfc3986#section-3.1), and must not already // have a factory registered. Zap automatically registers a factory for the // "file" scheme. funcRegisterSink(scheme string, factory func(*url.URL)(Sink, error)) error { _sinkMutex.Lock() defer _sinkMutex.Unlock()
if scheme == "" { return errors.New("can't register a sink factory for empty string") } normalized, err := normalizeScheme(scheme) if err != nil { return fmt.Errorf("%q is not a valid scheme: %v", scheme, err) } if _, ok := _sinkFactories[normalized]; ok { return fmt.Errorf("sink factory already registered for scheme %q", normalized) } _sinkFactories[normalized] = factory returnnil }
// A WriteSyncer is an io.Writer that can also flush any buffered data. Note // that *os.File (and thus, os.Stderr and os.Stdout) implement WriteSyncer. type WriteSyncer interface { io.Writer Sync() error }
// Writer is the interface that wraps the basic Write method. // // Write writes len(p) bytes from p to the underlying data stream. // It returns the number of bytes written from p (0 <= n <= len(p)) // and any error encountered that caused the write to stop early. // Write must return a non-nil error if it returns n < len(p). // Write must not modify the slice data, even temporarily. // // Implementations must not retain p. type Writer interface { Write(p []byte) (n int, err error) }
// Closer is the interface that wraps the basic Close method. // // The behavior of Close after the first call is undefined. // Specific implementations may document their own behavior. type Closer interface { Close() error }
logger.Info("logger construction succeeded", zap.String("key", "value"), zap.Int("number", 9), )
sugarLogger := logger.Sugar()
sugarLogger.Infow("logger construction succeeded", "key", "value", "number", 9, ) }
执行结果:
远程日志接收服务端:
1 2 3 4 5 6 7 8 9 10
请求方法: POST 请求地址: /log Body体内容: {"level":"info","time":"2020-03-25T14:48:29.221+0800","caller":"log/log2.go:117","message":"logger construction succeeded","app":"apdex","key":"value","number":9}
=========== 请求方法: POST 请求地址: /log Body体内容: {"level":"info","time":"2020-03-25T14:48:29.221+0800","caller":"log/log2.go:124","message":"logger construction succeeded","app":"apdex","key":"value","number":9}
===========
执行写日志的客户端:
1 2
{"level":"info","time":"2020-03-25T14:48:29.221+0800","caller":"log/log2.go:117","message":"logger construction succeeded","app":"apdex","key":"value","number":9} {"level":"info","time":"2020-03-25T14:48:29.221+0800","caller":"log/log2.go:124","message":"logger construction succeeded","app":"apdex","key":"value","number":9}