Oris_Log

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

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

Go to latest
Published: Mar 5, 2022 License: Apache-2.0 Imports: 17 Imported by: 0

README

Oris-Logger

Oris-logger allows users to log application logs to file, console or DB (NoSql/Sql) The logger users to add context to logs if needed.

Features

  1. Write log to SQL/NoSQL DB.
  2. Write log to file.
  3. Write log to console.
  4. Logs are written in json/plain text format.
  5. Logs can be written to multiple output source i.e. to both file and console.
  6. Context can be added to a log i.e. User ID, can be added to a log to track user footprint.

Documentation

Overview

Package Oris_Log allows users to write application logs to file, console or DB (NoSql/Sql)

The logger allows users to add context (context are more information that can be shared across the logger instance) to logs if needed.

Features

1. Write log to SQL/NoSQL DB.

2. Write log to file.

3. Write log to console.

4. Logs are written in json/plain text format.

5. Logs can be written to multiple output source i.e. to both file and console.

6. Context can be added to a log i.e. User ID, can be added to a log to track user footprint.

Oris logger requires a configuration file `logger.json`:

 {
 "name": "awesomeProject",
 "filename": "sample",
 "MaxFileSize": "2K",
 "folder": "logs",
 "output": "console",
 "buffer": 10000,
 "disable_logType": ["fatal","debug"]
	}

1. name: The project name

2. filename: The name that would be assigned to log file. This config is only needed when using file as output.

3. MaxFileSize: Set the max file size for each log, ones the file exceeds the max size, it is renamed and subsequent logs are written to a new file. Note: A new log file is created every day. The size is a string with suffix 'K' kilobyte and 'M' Megabyte.

4. folder: the name of the folder where logs file would be kept, the default location is ./logs.

5. output: This determines where logs would be written to i.e file, console, or MongoDB

6. buffer: This value is used when output is set to `file`, it buffers file during write to memory to avoid blocking.

7. disable_logType: This can be used to disable so log type in production i.e. debug log. The allowable values are 'info','debug','error','fatal'

Index

Constants

View Source
const (
	CONSOLE       = "console"
	FILE          = "file"
	SQL           = "sql"
	MONGODB       = "mongo"
	LOGDATEFORMAT = "2006-01-02 15:04:05"
	DATEFORMAT    = "2006_01_02"
	DBNAME        = "Log"

	MAXFILESIZE int64 = 214748364800

	INFO  Label = "INFO"
	ERROR Label = "ERROR"
	DEBUG Label = "DEBUG"
	FATAL Label = "FATAL"
)

Variables

This section is empty.

Functions

func ConvertSize

func ConvertSize(size string) int64

ConvertSize converts MaxFileSize value in the config file to bytes.

Types

type Configuration

type Configuration struct {
	Name           string
	Filename       string
	MaxFileSize    string
	Folder         string
	Output         string
	Buffer         int
	DisableLogType []string
	MongoDB        *mongo.Database
	ColName        string
}

type ConsoleWriter

type ConsoleWriter struct {
	ID string
	// contains filtered or unexported fields
}

ConsoleWriter writes log to console

func (*ConsoleWriter) AddContext

func (l *ConsoleWriter) AddContext(key string, value interface{})

AddContext Add a new context value to log writer

func (*ConsoleWriter) Debug

func (l *ConsoleWriter) Debug(arg0 interface{}, arg1 ...interface{})

Debug log debug

func (*ConsoleWriter) Error

func (l *ConsoleWriter) Error(arg0 interface{}, arg1 ...interface{})

Error log error

func (*ConsoleWriter) Fatal

func (l *ConsoleWriter) Fatal(arg0 interface{}, arg1 ...interface{})

Fatal log fatal

func (*ConsoleWriter) GetContext

func (l *ConsoleWriter) GetContext(key string) interface{}

GetContext returns context value based on its key

func (*ConsoleWriter) Info

func (l *ConsoleWriter) Info(arg0 interface{}, arg1 ...interface{})

Info log info

func (*ConsoleWriter) NewContext

func (l *ConsoleWriter) NewContext() Logger

NewContext This function is used to add context to the logger instance

func (*ConsoleWriter) SetLogID

func (l *ConsoleWriter) SetLogID(id string)

SetLogID set the current logger instance's ID

type FileWriter

type FileWriter struct {
	ID string
	sync.Mutex
	// contains filtered or unexported fields
}

FileWriter writes log to file

func (*FileWriter) AddContext

func (l *FileWriter) AddContext(key string, value interface{})

AddContext Add a new context value to log writer

func (*FileWriter) Debug

func (f *FileWriter) Debug(arg0 interface{}, arg1 ...interface{})

Debug log debug

func (*FileWriter) Error

func (f *FileWriter) Error(arg0 interface{}, arg1 ...interface{})

Error log error

func (*FileWriter) Fatal

func (f *FileWriter) Fatal(arg0 interface{}, arg1 ...interface{})

Fatal log fatal

func (*FileWriter) GetContext

func (l *FileWriter) GetContext(key string) interface{}

GetContext returns context value based on its key

func (*FileWriter) Info

func (f *FileWriter) Info(arg0 interface{}, arg1 ...interface{})

Info log info

func (*FileWriter) NewContext

func (f *FileWriter) NewContext() Logger

NewContext This function is used to add context to a log record.

func (*FileWriter) SetLogID

func (f *FileWriter) SetLogID(id string)

type Label

type Label string

Label datatype for log type

type Logger

type Logger interface {
	// Info Logs are written to an output with an info label
	Info(interface{}, ...interface{})

	// Error Logs are written to an output with an error label
	Error(interface{}, ...interface{})

	// Debug Logs are written to an output with a debug label
	Debug(interface{}, ...interface{})

	// Fatal : Logs is been written to output and program terminates immediately
	Fatal(interface{}, ...interface{})

	// NewContext This function is used to add context to a log record, a new instance of the log writer is returned.
	NewContext() Logger

	// AddContext Add a new context value to a log writer
	AddContext(key string, value interface{})

	// GetContext returns context value based on its key
	GetContext(key string) interface{}

	// SetLogID set ID for the current log context
	SetLogID(id string)
}

Logger An interface that states the requirements for a writer to be an instance of Logger. Every log writer must implement the Logger interface.

func New

func New(conf Configuration) Logger

New create a logger instance

type MongoWriter

type MongoWriter struct {
	ID string
	// contains filtered or unexported fields
}

MongoWriter

func (*MongoWriter) AddContext

func (l *MongoWriter) AddContext(key string, value interface{})

AddContext Add a new context value to log writer

func (*MongoWriter) Debug

func (m *MongoWriter) Debug(arg0 interface{}, arg1 ...interface{})

Debug log debug

func (*MongoWriter) Error

func (m *MongoWriter) Error(arg0 interface{}, arg1 ...interface{})

Error log error

func (*MongoWriter) Fatal

func (m *MongoWriter) Fatal(arg0 interface{}, arg1 ...interface{})

Fatal log fatal

func (*MongoWriter) GetContext

func (l *MongoWriter) GetContext(key string) interface{}

GetContext returns context value based on its key

func (*MongoWriter) Info

func (m *MongoWriter) Info(arg0 interface{}, arg1 ...interface{})

Info log info

func (*MongoWriter) NewContext

func (m *MongoWriter) NewContext() Logger

NewContext This function is used to add context to a log record.

func (*MongoWriter) SetLogID

func (m *MongoWriter) SetLogID(id string)

SetLogID set the current logger instance's ID

type SqlWriter

type SqlWriter struct {
	ID string
	// contains filtered or unexported fields
}

SqlWriter

func (*SqlWriter) AddContext

func (l *SqlWriter) AddContext(key string, value interface{})

AddContext Add a new context value to log writer

func (*SqlWriter) Debug

func (s *SqlWriter) Debug(arg0 interface{}, arg1 ...interface{})

Debug log debug

func (*SqlWriter) Error

func (s *SqlWriter) Error(arg0 interface{}, arg1 ...interface{})

Error log error

func (*SqlWriter) Fatal

func (s *SqlWriter) Fatal(arg0 interface{}, arg1 ...interface{})

Fatal log fatal

func (*SqlWriter) GetContext

func (l *SqlWriter) GetContext(key string) interface{}

GetContext returns context value based on its key

func (*SqlWriter) Info

func (s *SqlWriter) Info(arg0 interface{}, arg1 ...interface{})

Info log info

func (*SqlWriter) NewContext

func (s *SqlWriter) NewContext() Logger

NewContext This function is used to add context to a log record.

func (*SqlWriter) SetLogID

func (s *SqlWriter) SetLogID(id string)

SetLogID set the current logger instance's ID

Jump to

Keyboard shortcuts

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