logger

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

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

Go to latest
Published: Jun 6, 2021 License: MIT Imports: 11 Imported by: 2

README

Logo of the project

gologger

Build Status Go Report Card

Logger library for the gophers

An openionated logging library for golang.Gologger writes logs as json in the following format

{
    "time":"2016-11-10T16:11:46.59Z",
    "process_name":"sample_logger",
    "host_name":"sarath.local",
    "process_id":29422,
    "level":"ERROR",
    "file_name":"/home/sarath/go/src/github.com/sarathsp06/gologger/sample/main.go",
    "line_num":13,
    "log_msg":"error happened"
}

That much info will be how following will be written

logger.Error("error happened")

Installing / Getting started

Just like any other go library

go get github.com/sarathsp06/gologger

But if one needs to get a particular version then use

go get gopkg.in/sarathsp06/gologger.vx #x is the version Number

Developing

Here's a brief intro about what a developer must do in order to start developing the project further:

  1. Get it using go get or clone
  2. Make changes and make a pull reqeuest with Updated README if featue addition

Features

What's all the bells and whistles this project can perform?

  • It can log - Obviously
  • Easy buffered logging
  • You can set write to logger for redirecting logs
  • log message will contain much deeper details like line number of error , process id,host_name etc

How to use

This is how one may use the library

import the package

import logger "github.com/sarathsp06/gologger"

Initialize the package

if err := logger.InitLogger("INFO", ".", "sample_logger",true); err != nil {
		panic(err.Error())
}

Log as you wish with formated and normal message

logger.Error("error happened")
logger.Debug("Debug message",":Sasa","sasa")
logger.Info("error happened")
logger.Warning("error happened")
logger.Errorf("error happened %s", "Yo")
logger.Debugf("debug message : %s", "YoYo")

Explore more ....

Here is a sample code

package main

import (
	"os"
	logger "github.com/sarathsp06/gologger"
)

func main() {
	if err := logger.InitLogger("INFO", ".", "sample_logger",true); err != nil {
		panic(err.Error())
	}
	logger.SetLogWriter(os.Stdout)
	logger.Error("error happened")
	logger.Debug("Debug message")
	logger.Info("error happened")
	logger.Warning("error happened")
	logger.Errorf("error happened %s", "Yo")
	logger.Debugf("debug message : %s", "YoYo")
}

Licensing

"The code in this project is licensed under MIT license."

Documentation

Index

Constants

View Source
const (
	// MONITOR monitoring logs flag
	MONITOR string = "MON"
	// ERROR error logs flag
	ERROR string = "ERR"
	// WARNING Warning logs flag string
	WARNING string = "WAR"
	// INFO informative log message flag string
	INFO string = "INF"
	// DEBUG debug logs flag string
	DEBUG string = "DEB"
)
View Source
const (
	WHITE     = 0
	RED   int = iota + 31
	GREEN
	YELLOW
	BLUE
	MAGENTA
)

Constants representing colors to tty color constants to be used as "\033[COLORm" Eg: \033[31m for RED

Variables

LogColors is map of log level to color

View Source
var LogLevels = map[string]int{MONITOR: 0, ERROR: 1, WARNING: 2, INFO: 3, DEBUG: 4}

LogLevels defines loglevel priorities 0 highest and 3 lowest

Functions

func Debug

func Debug(message ...interface{})

Debug Debug log without formatting

func Debugf

func Debugf(message ...interface{})

Debugf Prints log with formatting

func Error

func Error(message ...interface{})

Error function for error logs without formatting

func Errorf

func Errorf(message ...interface{})

Errorf Prints log with formatting

func Flush

func Flush()

Flush flushes the data logs to log writer

func GetLogWriter

func GetLogWriter() (io.Writer, error)

GetLogWriter returns an io.Writer for log if already created returns the same otherwise creates a new buffered Writer and returns

func Info

func Info(message ...interface{})

Info info level logs without formatting

func Infof

func Infof(message ...interface{})

Infof Prints log with formatting

func InitLogger

func InitLogger(level, directory, process string, humanRedable bool) error

InitLogger initialise logger object with logWriter and log level

func IsTerminal

func IsTerminal() bool

IsTerminal checks if the stdout is a terminal or pipe

func Monitor

func Monitor(m *Metric)

Monitor accepts a metric and sends it across

func SetBufferSize

func SetBufferSize(bufferSize int) error

SetBufferSize sets the buffer size for buffering logs the logs will be flushed anyways upon stop of the service

func SetLogType

func SetLogType(logType string)

SetLogType sets the log type This is more like a lable saying the type of log some possible log types would be application,error,

func SetLogWriter

func SetLogWriter(writer io.Writer) error

SetLogWriter sets writer for log

func Warning

func Warning(message ...interface{})

Warning Warning level logs without formatting

func Warningf

func Warningf(message ...interface{})

Warningf Prints log with formatting

Types

type Log

type Log struct {
	ProcessName string `json:"service"`
	LogType     string `json:"logtype"`
	LogTime     string `json:"timestamp"`
	HostName    string `json:"host"`
	Msg         string `json:"message"`
	Level       string `json:"loglevel"`
	ProcessID   int    `json:"processid"`
	FileName    string `json:"filename"`
	LineNum     int    `json:"linenum"`
}

Log defines the structure of the log message or the log format

func GetLog

func GetLog() Log

GetLog returns log struct GetLog returns the log struct with essential common data filled in

func (Log) Human

func (log Log) Human() string

Human returns human readable log string /Here the filename is going to be just to

func (Log) String

func (log Log) String() string

String implements Stringer interface json encode the object and returns

type Logger

type Logger struct {
	LogLevel        int
	BufferSizeBytes int
	LogType         string
	// contains filtered or unexported fields
}

Logger struct to hold the log level and the Writer

func GetLogger

func GetLogger() (*Logger, error)

GetLogger returns the current default logger instance

func (Logger) Flush

func (l Logger) Flush()

Flush flushed the buffer

func (Logger) Log

func (l Logger) Log(depth int, level string, message string)

Log given the stack depth and level with an array of messages decides if to be Written to logs ans writes to log with FileName and LineNum taken from runtime Info

func (*Logger) SetBufferSize

func (l *Logger) SetBufferSize(bufferSizeBytes int) (err error)

SetBufferSize sets buffer size as bytes

func (*Logger) SetLogType

func (l *Logger) SetLogType(logType string) *Logger

SetLogType sets the log type This is more like a lable saying the type of log some possible log types would be application,error,

func (*Logger) SetLogWriter

func (l *Logger) SetLogWriter(writer io.Writer) error

SetLogWriter sets default writer

func (*Logger) Writer

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

Writer returns writer to be used

type Metric

type Metric struct {
	Name   string                 `json:"name"`
	Tags   map[string]string      `json:"tags"`
	Fields map[string]interface{} `json:"fields"`
}

Metric format for metrics

func NewMetric

func NewMetric(name string) *Metric

NewMetric creates and initialises the metric

func (*Metric) Field

func (m *Metric) Field(key string, value interface{}) *Metric

Field sets field

func (Metric) String

func (m Metric) String() string

String implements Stringer interface json encode the object and returns

func (*Metric) Tag

func (m *Metric) Tag(key, value string) *Metric

Tag sets a tag

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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