1 // Copyright (c) 2016 Uber Technologies, Inc.
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 // Core is a minimal, fast logger interface. It's designed for library authors
24 // to wrap in a more user-friendly API.
28 // With adds structured context to the Core.
30 // Check determines whether the supplied Entry should be logged (using the
31 // embedded LevelEnabler and possibly some extra logic). If the entry
32 // should be logged, the Core adds itself to the CheckedEntry and returns
35 // Callers must use Check before calling Write.
36 Check(Entry, *CheckedEntry) *CheckedEntry
37 // Write serializes the Entry and any Fields supplied at the log site and
38 // writes them to their destination.
40 // If called, Write should always log the Entry and Fields; it should not
41 // replicate the logic of Check.
42 Write(Entry, []Field) error
43 // Sync flushes buffered logs (if any).
49 // NewNopCore returns a no-op Core.
50 func NewNopCore() Core { return nopCore{} }
51 func (nopCore) Enabled(Level) bool { return false }
52 func (n nopCore) With([]Field) Core { return n }
53 func (nopCore) Check(_ Entry, ce *CheckedEntry) *CheckedEntry { return ce }
54 func (nopCore) Write(Entry, []Field) error { return nil }
55 func (nopCore) Sync() error { return nil }
57 // NewCore creates a Core that writes logs to a WriteSyncer.
58 func NewCore(enc Encoder, ws WriteSyncer, enab LevelEnabler) Core {
72 func (c *ioCore) With(fields []Field) Core {
74 addFields(clone.enc, fields)
78 func (c *ioCore) Check(ent Entry, ce *CheckedEntry) *CheckedEntry {
79 if c.Enabled(ent.Level) {
80 return ce.AddCore(ent, c)
85 func (c *ioCore) Write(ent Entry, fields []Field) error {
86 buf, err := c.enc.EncodeEntry(ent, fields)
90 _, err = c.out.Write(buf.Bytes())
95 if ent.Level > ErrorLevel {
96 // Since we may be crashing the program, sync the output. Ignore Sync
97 // errors, pending a clean solution to issue #370.
103 func (c *ioCore) Sync() error {
107 func (c *ioCore) clone() *ioCore {
109 LevelEnabler: c.LevelEnabler,