simplelog

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2024 License: MIT Imports: 3 Imported by: 25

README

simplelog

Overview

Simplelog is a simple encapsulation of the standard library "log" Because I need to be able to control the level of logging.

Install

go get github.com/vearne/simplelog

Use environment variables to set log level

optional value: debug | info | warn | error

export SIMPLE_LOG_LEVEL=info

Usage

package main

import (
	slog "github.com/vearne/simplelog"
	"os"
)

func main() {
	logFile := "/var/log/simple.log"
	f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		slog.Fatal("error opening file: %v", err)
	}
	slog.SetOutput(f)
	defer f.Close()
	// set log level
	slog.SetLevel(slog.DebugLevel)

	slog.Debug("log, %v", "debug")
	slog.Info("log, %v", "info")
	slog.Error("log, %v", "error")
	// Like log, Fatal() will terminal process
	slog.Fatal("log, %v", "fatal")
}

Output

2020/05/27 17:40:01 [debug] log, debug
2020/05/27 17:40:01 [info] log, info
2020/05/27 17:40:01 [error] log, error
2020/05/27 17:40:01 [fatal] log, fatal

Documentation

Index

Constants

View Source
const (
	DebugLevel = iota
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
)

Variables

View Source
var (
	Level  = WarnLevel
	LogMap map[string]int
)

Functions

func Debug

func Debug(format string, v ...interface{})

func Error

func Error(format string, v ...interface{})

func Fatal

func Fatal(format string, v ...interface{})

func Info

func Info(format string, v ...interface{})

func SetLevel

func SetLevel(loglevel int)

func SetOutput

func SetOutput(w io.Writer)

func Warn

func Warn(format string, v ...interface{})

Types

This section is empty.

Jump to

Keyboard shortcuts

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