mlog

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

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

Go to latest
Published: Aug 5, 2018 License: MIT Imports: 7 Imported by: 27

README

A simple logging module for go, with a rotating file feature and console logging.

Installation

go get github.com/jbrodriguez/mlog

Usage

Sample usage

Write to stdout/stderr and create a rotating logfile

package main

import "github.com/jbrodriguez/mlog"

func main() {
	mlog.Start(mlog.LevelInfo, "app.log")

	mlog.Info("Hello World !")

	ipsum := "ipsum"
	mlog.Warning("Lorem %s", ipsum)
}

Write to stdout/stderr only

package main

import "github.com/jbrodriguez/mlog"

func main() {
	mlog.Start(mlog.LevelInfo, "")

	mlog.Info("Hello World !")

	ipsum := "ipsum"
	mlog.Warning("Lorem %s", ipsum)
}

By default, the log will be rolled over to a backup file when its size reaches 10Mb and 10 such files will be created (and eventually reused).

Alternatively, you can specify the max size of the log file before it gets rotated, and the number of backup files you want to create, with the StartEx function.

package main

import "github.com/jbrodriguez/mlog"

func main() {
    mlog.StartEx(mlog.LevelInfo, "app.log", 5*1024*1024, 5)

    mlog.Info("Hello World !")

    ipsum := "ipsum"
    mlog.Warning("Lorem %s", ipsum)
}

This will rotate the file when it reaches 5Mb and 5 backup files will eventually be created.

Setting logger flags:

mlog.DefaultFlags = log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile

Output

I: 2015/05/15 07:09:45 main.go:10: Hello World !
W: 2015/05/15 07:09:45 main.go:13: Lorem ipsum

Documentation

Overview

Package mlog is simple logging module for go, with a rotating file feature and console logging.

Index

Constants

View Source
const BackupCount int = 10
View Source
const MaxBytes int = 10 * 1024 * 1024

Variables

View Source
var DefaultFlags = log.Ldate | log.Ltime | log.Lshortfile

DefaultFlags used by created loggers

View Source
var Logger mlog

Functions

func Error

func Error(err error)

Error writes to the Error destination and accepts an err

func Fatal

func Fatal(a ...interface{})

Fatal writes to the Fatal destination and exits with an error 255 code

func FatalIfError

func FatalIfError(err error)

FatalIfError is a shortcut function for log.Fatalf if error and exits with an error 255 code

func Fatalf

func Fatalf(format string, a ...interface{})

Fatalf writes to the Fatal destination and exits with an error 255 code

func IfError

func IfError(err error)

IfError is a shortcut function for log.Error if error

func Info

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

Info writes to the Info destination

func Start

func Start(level LogLevel, path string)

Start starts the logging

func StartEx

func StartEx(level LogLevel, path string, maxBytes, backupCount int)

func Stop

func Stop() error

Stop stops the logging

func Sync

func Sync()

Sync commits the current contents of the file to stable storage. Typically, this means flushing the file system's in-memory copy of recently written data to disk.

func Trace

func Trace(format string, a ...interface{})

Trace writes to the Trace destination

func Warning

func Warning(format string, a ...interface{})

Warning writes to the Warning destination

Types

type LogLevel

type LogLevel int32

LogLevel type

const (
	// LevelTrace logs everything
	LevelTrace LogLevel = (1 << iota)

	// LevelInfo logs Info, Warnings and Errors
	LevelInfo

	// LevelWarn logs Warning and Errors
	LevelWarn

	// LevelError logs just Errors
	LevelError
)

type RotatingFileHandler

type RotatingFileHandler struct {
	// contains filtered or unexported fields
}

RotatingFileHandler writes log a file, if file size exceeds maxBytes, it will backup current file and open a new one.

max backup file number is set by backupCount, it will delete oldest if backups too many.

func NewRotatingFileHandler

func NewRotatingFileHandler(fileName string, maxBytes int, backupCount int) (*RotatingFileHandler, error)

NewRotatingFileHandler creates dirs and opens the logfile

func (*RotatingFileHandler) Close

func (h *RotatingFileHandler) Close() error

Close simply closes the File

func (*RotatingFileHandler) Write

func (h *RotatingFileHandler) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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