auralog

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: May 13, 2023 License: BSD-2-Clause Imports: 7 Imported by: 3

README

auralog

GoDoc

A Go logging package based off Go's Log package with some differences. This has about as much overhead as the stock Log package because it doesn't use thirdparty packages like the stock package. The differences it has over the stock Go log library are below:

  • Different logging levels INFO, WARN, ERROR, FATAL, PANIC, DEBUG
  • Config struct for cleaner initialization.
  • Removed helpers so it has to be manually.
  • Built in Rotate Writer io.Writer interface for basic log file rotation.

Getting Started

go get github.com/saintwish/auralog Will get the latest release.

Documentation

GoDoc

Example

This is a basic example of how you will initialize the logger and use it with the RotateWriter.

package main

import(
  "os"
  "io"
  "time"

  "github.com/saintwish/auralog"
)

var (
  flags = auralog.Ldate | auralog.Ltime
  wflags = auralog.Ldate | auralog.Ltime
  eflags = auralog.Ldate | auralog.Ltime | auralog.Lshortfile
  dflags = auralog.Ldate | auralog.Ltime | auralog.Lshortfile

  log *auralog.Logger
)

func main() {
  file := &auralog.RotateWriter{
    Dir: "./runtime/logs/", //Required
    Filename: "log.log", //Required
    ExTime: 24 * time.Hour, //Required if you want daily log rotation.
    MaxSize: 5 * auralog.Megabyte, //Can also use auralog.Kilobyte, Not required
  }

  log = auralog.New(auralog.Config{
    Output: io.MultiWriter(file, os.Stdout), //Required
    Prefix: "[PREFIX] ", //Not Required
    Level: auralog.LogLevelInfo, //Required
    Flag: flags, //Required
    WarnFlag: wflags, //Required
    ErrorFlag: eflags, //Required
    DebugFlag: dflags, //Required
  })
  log.Println("Test")
  log.Warnln("TEST WARN")
  log.Errorln("TEST ERROR")
  log.Debugln("TEST DEBUG")
}

Contributing

If you have any additions or contributions you would like to make please do. Just keep the below in mind.

  • Try to match current naming conventions as closely as possible.
  • Create a Pull Request with your changes against the master branch.

License

It's licensed under BSD-2-Clause License to stay compatible with Go's stock log package, since it's created from that.

Documentation

Overview

Package auralog is a modified Go log package with level logging and RotateWriter to automatically rotate log files. Also included a config struct to make creating a new logger cleaner, and the helper functions have been removed. The different levels are the below constants.

Index

Constants

View Source
const (
	Ldate         = 1 << iota     // the date in the local time zone: 2009/01/23
	Ltime                         // the time in the local time zone: 01:23:23
	Lmicroseconds                 // microsecond resolution: 01:23:23.123123.  assumes Ltime.
	Llongfile                     // full file name and line number: /a/b/c/d.go:23
	Lshortfile                    // final file name element and line number: d.go:23. overrides Llongfile
	LUTC                          // if Ldate or Ltime is set, use UTC rather than the local time zone
	Lmsgprefix                    // move the "prefix" from the beginning of the line to before the message
	LstdFlags     = Ldate | Ltime // initial values for the standard logger
)

These flags define which text to prefix to each log entry generated by the Logger. Bits are or'ed together to control what's printed. With the exception of the Lmsgprefix flag, 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

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Output io.Writer // io.Writer of what to log to.
	Prefix string    // prefix on each line to identify the logger (but see Lmsgprefix)
	Level  LogLevel  // What log level should be print.

	Flag      int
	WarnFlag  int // Required, flags for warn log.
	ErrorFlag int // Required, flags for error, fatal, and panic logs.
	DebugFlag int // Required, flags for debug logs.
	TraceFlag int // Required, flags for trace logs.
}

A structure for cleaner code when initalizing Logger struct.

type LogLevel

type LogLevel int
const (
	LogLevelInfo LogLevel = iota
	LogLevelWarn
	LogLevelError
	LogLevelFatal
	LogLevelPanic
	LogLevelDebug
	LogLevelTrace
)

Constants for log levels.

LogLevelInfo - Prints only info logs.
LogLevelWarn - Prints info and warn logs.
LogLevelError - Prints info, warn, and error logs.
LogLevelFatal - Prints info, warn, error, fatal logs.
LogLevelPanic - Prints info, warn, error, fatal, panic logs.
LogLevelDebug - Prints info, warn, error, fatal, panic, debug logs.
LogLevelTrace - Prints info, warn, error, fatal, panic, debug, trace logs.

func ToLogLevel added in v1.0.1

func ToLogLevel(level string) LogLevel

ToLogLevel(level string) return the log level constant from a string.

Example: ToLogLevel("warn") will return LogLevelWarn constant.

func (LogLevel) String

func (l LogLevel) String() string

LogLevel.String returns the LogLevel string.

Example: LogLevelWarn.String() will return "WARN: " string.

type Logger

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

A Logger represents an active logging object that generates lines of output to an io.Writer. Each logging operation makes a single call to the Writer's Write method. A Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Writer.

func New

func New(c Config) *Logger

New creates a new Logger. The out variable sets the destination to which log data will be written. The prefix appears at the beginning of each generated log line, or after the log header if the Lmsgprefix flag is provided. The flag argument defines the logging properties.

func (*Logger) Debug

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

Debug level logging. Debug is equivalent to l.Print()

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

Debug level logging. Debugf is equivalent to l.Printf()

func (*Logger) Debugln

func (l *Logger) Debugln(v ...interface{})

Debug level logging. Debugln is equivalent to l.Println()

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

Error level logging. Error calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

Error level logging. Errorf calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Errorln

func (l *Logger) Errorln(v ...interface{})

Error level logging. Errorln calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Println.

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

Fatal is equivalent to l.Print() followed by a call to os.Exit(1).

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, v ...interface{})

Fatalf is equivalent to l.Printf() followed by a call to os.Exit(1).

func (*Logger) Fatalln

func (l *Logger) Fatalln(v ...interface{})

Fatalln is equivalent to l.Println() followed by a call to os.Exit(1).

func (*Logger) Flags

func (l *Logger) Flags() int

Flags returns the output flags for the logger. The flag bits are Ldate, Ltime, and so on.

func (*Logger) Info added in v1.0.4

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

An alias for Print

func (*Logger) Infof added in v1.0.4

func (l *Logger) Infof(format string, v ...interface{})

An alias for Print

func (*Logger) Infoln added in v1.0.4

func (l *Logger) Infoln(v ...interface{})

An alias for Print

func (*Logger) Output

func (l *Logger) Output(level LogLevel, s string) error

Output writes the output for a logging event. The string s contains the text to print after the prefix specified by the flags of the Logger. A newline is appended if the last character of s is not already a newline.

func (*Logger) Panic

func (l *Logger) Panic(v ...interface{})

Panic is equivalent to l.Print() followed by a call to panic().

func (*Logger) Panicf

func (l *Logger) Panicf(format string, v ...interface{})

Panicf is equivalent to l.Printf() followed by a call to panic().

func (*Logger) Panicln

func (l *Logger) Panicln(v ...interface{})

Panicln is equivalent to l.Println() followed by a call to panic().

func (*Logger) Prefix

func (l *Logger) Prefix() string

Prefix returns the output prefix for the logger.

func (*Logger) Print

func (l *Logger) Print(v ...interface{})

Print calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Printf

func (l *Logger) Printf(format string, v ...interface{})

Printf calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Println

func (l *Logger) Println(v ...interface{})

Println calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Println.

func (*Logger) SetFlags

func (l *Logger) SetFlags(flag int)

SetFlags sets the output flags for the logger. The flag bits are Ldate, Ltime, and so on.

func (*Logger) SetLevel added in v1.0.5

func (l *Logger) SetLevel(level LogLevel)

SetLevel sets the log level for the logger.

func (*Logger) SetOutput

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

SetOutput sets the output destination for the logger.

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(prefix string)

SetPrefix sets the output prefix for the logger.

func (*Logger) Trace added in v1.0.4

func (l *Logger) Trace(v ...interface{})

Trace level logging. Trace is equivalent to l.Print()

func (*Logger) Tracef added in v1.0.4

func (l *Logger) Tracef(format string, v ...interface{})

Trace level logging. Tracef is equivalent to l.Printf()

func (*Logger) Traceln added in v1.0.4

func (l *Logger) Traceln(v ...interface{})

Trace level logging. Traceln is equivalent to l.Println()

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

Warn level logging. Warn calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...interface{})

Warn level logging. Warnf calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Warnln

func (l *Logger) Warnln(v ...interface{})

Warn level logging. Warnln calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Println.

func (*Logger) Writer

func (l *Logger) Writer() io.Writer

Writer returns the output destination for the logger.

Directories

Path Synopsis
RotateWriter interfaces to io.Writer to automaticly rotate the log file based on max size and/or time
RotateWriter interfaces to io.Writer to automaticly rotate the log file based on max size and/or time

Jump to

Keyboard shortcuts

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