Help us shape the future of Go! Take the 2026 Go Developer Survey

tlog

package module
v0.0.0-...-3ddceff Latest Latest
Warning

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

Go to latest
Published: May 25, 2015 License: MIT Imports: 5 Imported by: 1

README

tlog wercker status

Package tlog provides simple APIs to write log to console and file simultaneously.

Tlog is a wrapper around golang's standard log package.

Features:

  • Predefined log levels, from debug to critical.
  • Console and logfile loglevel can be set separately.
  • Console log writes to stderr or stdout according to log level.

Log levels:

  • DEBUG - 1
  • INFO - 2
  • WARN - 3
  • ERROR - 4
  • CRITICAL - 5
  • OFF - 6
Usage
package main

import (
  "log"
  "github.com/txchen/tlog"
)

func main() {
  // By default, only log to console
  tlog.WARN.Println("warn")

  // Default console loglevel is WARN, we can change it
  tlog.SetConsoleLogLevel(tlog.LevelInfo)
  tlog.INFO.Println("now I can show")

  // Set logfile to enable file based logging
  tlog.SetLogFile("z.log")
  tlog.CRITICAL.Printf("2 + 2 = %d", 4)

  // By default, logfile loglevel is INFO, let's change it
  tlog.SetLogfileLogLevel(tlog.LevelDebug)
  tlog.Debug.Println("you can only see me in file")

  // By default, tlog use Ldate | Ltime flag, you can tune it
  tlog.SetLogFlags(log.Ldate | log.Ltime | log.Lshortfile)
  // Now you can see different logging style
  tlog.ERROR.Println("different style")

  // And you can turn off the flag
  tlog.SetLogFlags(0)
  // Now the output will be "WARN: clean"
  tlog.WARN.Println("clean")

  // To turn off logging, set the level to off
  tlog.SetLogfileLogLevel(tlog.LevelOff)
  tlog.SetConsoleLogLevel(tlog.LevelOff)
  tlog.CRITICAL.Println("no one can see me")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DEBUG    *log.Logger
	INFO     *log.Logger
	WARN     *log.Logger
	ERROR    *log.Logger
	CRITICAL *log.Logger

	LogFileWriter           = ioutil.Discard
	StdoutWriter  io.Writer = os.Stdout
	StderrWriter  io.Writer = os.Stderr
)

tlog exposes *log.Logger instances which can be used to do actual logging.

Functions

func SetConsoleLogLevel

func SetConsoleLogLevel(level LogLevel)

SetConsoleLogLevel sets the console loglevel, matching or above will be logged. set to LevelOff to turn off console logging.

func SetLogFile

func SetLogFile(path string) error

SetLogFile takes filename as input and enables logfile logging, log will be appended to the file.

func SetLogFlags

func SetLogFlags(flag int)

SetLogFlags takes "log" package's log flags, sets output flags for the logger.

func SetLogfileLogLevel

func SetLogfileLogLevel(level LogLevel)

SetLogfileLogLevel sets the logfile loglevel, matching or above will be logged. set to LevelOff to turn off logfile logging.

func UseTempLogFile

func UseTempLogFile(prefix string) error

UseTempLogFile creates a temporary file and set it as the logfile name.

Types

type LogLevel

type LogLevel int

LogLevel 1-5, from debug to critical, 6 is off

const (
	LevelDebug LogLevel = iota
	LevelInfo
	LevelWarn
	LevelError
	LevelCritical
	LevelOff
	DefaultLogfileThreshold = LevelInfo
	DefaultConsoleThreshold = LevelWarn
)

These are the log levels defined in tlog.

func ConsoleLogLevel

func ConsoleLogLevel() LogLevel

ConsoleLogLevel returns current console threashold.

func LogfileLogLevel

func LogfileLogLevel() LogLevel

LogfileLogLevel returns current logfile threashold

Jump to

Keyboard shortcuts

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