author | Alberto Bertogli
<albertito@blitiri.com.ar> 2017-08-13 18:28:18 UTC |
committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2017-08-13 18:28:18 UTC |
parent | 4e4951f8e9ea08d1ab330cddbec60c4038bdf40d |
log.go | +17 | -17 |
log_test.go | +9 | -9 |
diff --git a/log.go b/log.go index 10a7d9c..7981aab 100644 --- a/log.go +++ b/log.go @@ -59,10 +59,10 @@ var levelToLetter = map[Level]string{ // A Logger represents a logging object that writes logs to the given writer. type Logger struct { - Level Level - LogTime bool + level Level + logTime bool - CallerSkip int + callerSkip int w io.WriteCloser sync.Mutex @@ -71,9 +71,9 @@ type Logger struct { func New(w io.WriteCloser) *Logger { return &Logger{ w: w, - CallerSkip: 0, - Level: Info, - LogTime: true, + callerSkip: 0, + level: Info, + logTime: true, } } @@ -84,7 +84,7 @@ func NewFile(path string) (*Logger, error) { } l := New(f) - l.LogTime = true + l.logTime = true return l, nil } @@ -95,7 +95,7 @@ func NewSyslog(priority syslog.Priority, tag string) (*Logger, error) { } l := New(w) - l.LogTime = false + l.logTime = false return l, nil } @@ -104,7 +104,7 @@ func (l *Logger) Close() { } func (l *Logger) V(level Level) bool { - return level <= l.Level + return level <= l.level } func (l *Logger) Log(level Level, skip int, format string, a ...interface{}) { @@ -116,7 +116,7 @@ func (l *Logger) Log(level Level, skip int, format string, a ...interface{}) { msg := fmt.Sprintf(format, a...) // Caller. - _, file, line, ok := runtime.Caller(1 + l.CallerSkip + skip) + _, file, line, ok := runtime.Caller(1 + l.callerSkip + skip) if !ok { file = "unknown" } @@ -134,7 +134,7 @@ func (l *Logger) Log(level Level, skip int, format string, a ...interface{}) { msg = letter + " " + msg // Time. - if l.LogTime { + if l.logTime { msg = time.Now().Format("20060102 15:04:05.000000 ") + msg } @@ -169,9 +169,9 @@ func (l *Logger) Fatalf(format string, a ...interface{}) { // The default logger, used by the top-level functions below. var Default = &Logger{ w: os.Stderr, - CallerSkip: 1, - Level: Info, - LogTime: false, + callerSkip: 1, + level: Info, + logTime: false, } // Init the default logger, based on the command-line flags. @@ -196,9 +196,9 @@ func Init() { Default.w = multiWriteCloser(Default.w, os.Stderr) } - Default.CallerSkip = 1 - Default.Level = Level(*vLevel) - Default.LogTime = *logTime + Default.callerSkip = 1 + Default.level = Level(*vLevel) + Default.logTime = *logTime } func V(level Level) bool { diff --git a/log_test.go b/log_test.go index 9c9763c..28d9dc4 100644 --- a/log_test.go +++ b/log_test.go @@ -35,7 +35,7 @@ func checkContentsMatch(t *testing.T, name, path, expected string) { } func testLogger(t *testing.T, fname string, l *Logger) { - l.LogTime = false + l.logTime = false l.Infof("message %d", 1) checkContentsMatch(t, "info-no-time", fname, "^_ log_test.go:.... message 1\n") @@ -46,43 +46,43 @@ func testLogger(t *testing.T, fname string, l *Logger) { "^_ log_test.go:.... message 1\n") os.Truncate(fname, 0) - l.LogTime = true + l.logTime = true l.Infof("message %d", 1) checkContentsMatch(t, "info-with-time", fname, `^\d{8} ..:..:..\.\d{6} _ log_test.go:.... message 1\n`) os.Truncate(fname, 0) - l.LogTime = false + l.logTime = false l.Errorf("error %d", 1) checkContentsMatch(t, "error", fname, `^E log_test.go:.... error 1\n`) if l.V(Debug) { - t.Fatalf("Debug level enabled by default (level: %v)", l.Level) + t.Fatalf("Debug level enabled by default (level: %v)", l.level) } os.Truncate(fname, 0) - l.LogTime = false + l.logTime = false l.Debugf("debug %d", 1) checkContentsMatch(t, "debug-no-log", fname, `^$`) os.Truncate(fname, 0) - l.Level = Debug + l.level = Debug l.Debugf("debug %d", 1) checkContentsMatch(t, "debug", fname, `^\. log_test.go:.... debug 1\n`) if !l.V(Debug) { - t.Errorf("l.Level = Debug, but V(Debug) = false") + t.Errorf("l.level = Debug, but V(Debug) = false") } os.Truncate(fname, 0) - l.Level = Info + l.level = Info l.Log(Debug, 0, "log debug %d", 1) l.Log(Info, 0, "log info %d", 1) checkContentsMatch(t, "log", fname, `^_ log_test.go:.... log info 1\n`) os.Truncate(fname, 0) - l.Level = Info + l.level = Info l.Log(Fatal, 0, "log fatal %d", 1) checkContentsMatch(t, "log", fname, `^☠ log_test.go:.... log fatal 1\n`)