lopher

package module
v0.0.0-...-ea392ea Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2018 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Lopher

GoDoc Go Walker Gocover Go Report Card wercker status

A dead simple log library, inspired by Dave Cheney's blog post on logging.

Only two log levels exist: Info and Debug. Info is always enabled, Debug is for development time.

Included are the same flags that are available with the standard library log package, with an API that should feel familiar to you if you've used the standard library log package at all.

How to use

package main

import (
    "time"
    "os"
    "gopkg.in/adamveld12/lopher.v1"
)

func main(){
    // make a new instance:
    l := lopher.New(os.Stdout, false, lopher.LFstdFlags)

    started := time.Now()

    // mm/dd/yyyy hh:mm:ss /file/path/main.go [INFO] App started.\n
    l.Info("App started.")

    // Debug entries don't print unless you set DebugMode to true
    l.DebugMode(true)

    l.Debug("App is doing stuff")
    l.Debugf("App ran for %+v", time.Since(started))

    // Also included are package level functions with the same API as an instance
    lopher.Info("App Exiting")
}

Credits

Dave Cheney for being dope

License

MIT

Documentation

Overview

Package lopher is inspired by Dave Cheney's blog post on logging: https://dave.cheney.net/2015/11/05/lets-talk-about-logging

lopher only supports two logging levels:

1. Things that developers care about when they are developing or debugging software. (DEBUG) 2. Things that users care about when using your software. (INFO)

If you need to log errors, use `Info`. If you need to log warnings, use `Debug“ or don't log them (most likely no one will care). If you need log.Fatal, bubble the error up to `main.main` and gracefully exit there; it makes the most sense and is cleaner than the alternatives.

Example
l := New(os.Stdout, false, "App: ", LFNone)
started := time.Now()
l.Info("Started.")

l.Debug("Taking time measurement...")

// Setting debug enables the debug level
l.SetDebug(true)

time.Sleep(time.Second)
s := math.Floor(time.Since(started).Seconds())
l.Debugf("ran for %v seconds.", s)
l.Info("Exiting.")
Output:
[INFO] App: Started.
[DEBUG] App: ran for 1 seconds.
[INFO] App: Exiting.
Example (Package)
SetFlags(LFNone)
SetOutput(os.Stdout)
started := time.Now()
Info("App Started.")

Debug("Taking time measurement...")

// Setting debug enables the debug level
SetDebug(true)

time.Sleep(time.Second)
s := math.Floor(time.Since(started).Seconds())
Debugf("App ran for %.3f minutes.", time.Since(started).Minutes())
Infof("App Exiting after %v second.", s)
Output:
[INFO] App Started.
[DEBUG] App ran for 0.017 minutes.
[INFO] App Exiting after 1 second.

Index

Examples

Constants

View Source
const (
	// Bits or'ed together to control what's printed.
	// There is no control over the order they appear (the order listed
	// here) or the format they present (as described in the comments).
	// The prefix is followed by a colon only when Llongfile or Lshortfile
	// is specified.
	// For example, flags Ldate | Ltime (or LstdFlags) produce,
	//	2009/01/23 01:23:23 message
	// while flags Ldate | Ltime | Lmicroseconds | Llongfile produce,
	//	2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
	LFdate         = 1 << iota                            // the date in the local time zone: 2009/01/23
	LFtime                                                // the time in the local time zone: 01:23:23
	LFmicroseconds                                        // microsecond resolution: 01:23:23.123123.  assumes Ltime.
	LFlongfile                                            // full file name and line number: /a/b/c/d.go:23
	LFshortfile                                           // final file name element and line number: d.go:23. overrides Llongfile
	LFUTC                                                 // if Ldate or Ltime is set, use UTC rather than the local time zone
	LFstdFlags     = LFdate | LFtime | LFUTC | LFlongfile // initial values for the standard logger
	LFNone         = 0
)

These flags define which text to prefix to each log entry generated by the Logger.

Variables

This section is empty.

Functions

func Debug

func Debug(v ...interface{})

Debug logs at debug level

func Debugf

func Debugf(fmt string, v ...interface{})

Debugf logs a formatted string at debug level

func Info

func Info(v ...interface{})

Info logs at info level

func Infof

func Infof(fmt string, v ...interface{})

Infof logs a formatted string at info level

func SetDebug

func SetDebug(debug bool)

SetDebug sets debug mode

func SetFlags

func SetFlags(f LogFlags)

SetFlags sets the log entry prefix flags

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets the output writer for the logger

Types

type Log

type Log struct {
	sync.Mutex
	// Output is the writer to send logs to
	io.Writer
	// Debug when true will enable the DEBUG logging level
	DebugMode bool
	// Flags are a set of flags to use for setting up log time stamps
	Flags LogFlags
	// Prefix is the prefix string that is prepended before each log line
	Prefix string
}

Log is a simple implementation of Logger

func (*Log) Debug

func (l *Log) Debug(v ...interface{})

Debug logs at debug level

func (*Log) Debugf

func (l *Log) Debugf(fmtStr string, v ...interface{})

Debugf logs a formatted string at debug level

func (*Log) Info

func (l *Log) Info(v ...interface{})

Info logs at info level

func (*Log) Infof

func (l *Log) Infof(fmtStr string, v ...interface{})

Infof logs a formatted string at info level

func (*Log) SetDebug

func (l *Log) SetDebug(v bool)

SetDebug enables or disables debug printing

func (*Log) SetFlags

func (l *Log) SetFlags(f LogFlags)

SetFlags sets the log flags used by the Logger

func (*Log) SetOutput

func (l *Log) SetOutput(w io.Writer)

SetOutput sets the output writer for the Logger

func (*Log) SetPrefix

func (l *Log) SetPrefix(prefixStr string)

SetPrefix sets the logger's prefix string

type LogFlags

type LogFlags int

LogFlags are a set of flags which define the prefix for each log entry

type Logger

type Logger interface {
	// Info logs at info level
	Info(...interface{})

	// Infof logs a formatted string at info level
	Infof(string, ...interface{})

	// Debug logs at debug level
	Debug(...interface{})

	// Debugf logs a formatted string at debug level
	Debugf(string, ...interface{})

	// SetFlags sets the log entry prefix flags
	SetFlags(LogFlags)

	// SetOutput sets the output writer for the logger
	SetOutput(io.Writer)

	// SetDebug sets debug mode
	SetDebug(bool)

	// SetPrefix sets the logger's prefix text that is prepended to each log line
	SetPrefix(string)
}

Logger is a a simple logger interface that only supports two levels: INFO and DEBUG

func New

func New(out io.Writer, debug bool, prefix string, flags LogFlags) Logger

New creates a new Logger Loggers are immutable

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL