tolog

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2024 License: MIT Imports: 6 Imported by: 11

README

Tolog

Just a log package. Have to create a logs dir in your project.

Usage

Basic
    tolog.Debug("debug").PrintAndWriteSafe()
    tolog.Infof("info").PrintAndWriteSafe()
Options
    tolog.Log(WithType("info"), WithContext("Info message")).PrintAndWriteSafe()
Multiple
    tolog.Info("Info message").PrintAndWriteSafe()
    tolog.Infof("Infof message %s","string").PrintAndWriteSafe()
    tolog.Infoln("Infoln message", "this is message").PrintAndWriteSafe()

Log level

  • Info
  • Warning
  • Error
  • Debug
  • Notice
  • Unknown

Log setting

  • logFileDateFormat
  • logTimeFormat
  • LogfilePrefix
  • LogWithColor
  • channelSize
  • logTicker

Log setting function

    SetLogWithColor(bool)
    SetLogPrefix(string)
    SetLogChannelSize(int)
    SetLogTickerTime(time.Duration)
    SetLogFileDateFormat(format DateFormat)
    SetLogTimeFormat(format DateFormat)
    SetLogTimezone(*time.Location)

Print & Write

    PrintAndWriteSafe()
    WriteSafe()
    Print()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LogTimeZone = time.Local

LogTimeZone The time zoon logger will print time at. Default is Local.

View Source
var LogWithColor = true

LogWithColor The variable of whether to use color in the log, default is true.

View Source
var LogfilePrefix = ""

LogfilePrefix The prefix of the log file, default is null. Use set prefix to set.

Functions

func CloseLogFile

func CloseLogFile()

CloseLogFile closes the log file.

func CreateFullLog

func CreateFullLog(l *ToLog)

CreateFullLog creates the full log message by combining log time, type, and context.

func SetLogChannelSize

func SetLogChannelSize(size int)

SetLogChannelSize set the size of go channel for cache.

func SetLogFileDateFormat

func SetLogFileDateFormat(format DateFormat)

SetLogFileDateFormat sets the date format for log file.

func SetLogPrefix

func SetLogPrefix(prefix string)

SetLogPrefix sets the log file prefix.

func SetLogTickerTime

func SetLogTickerTime(duration time.Duration)

SetLogTickerTime set the duration of saving log to file.

func SetLogTimeFormat

func SetLogTimeFormat(format DateFormat)

SetLogTimeFormat sets the date format for log time.

func SetLogTimeZone added in v1.0.3

func SetLogTimeZone(zone *time.Location)

SetLogTimeZone sets the time zone for log time.

func SetLogWithColor

func SetLogWithColor(flag bool)

SetLogWithColor sets the log shows colors or not.

Types

type DateFormat

type DateFormat string
const (
	Layout      DateFormat = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order.
	ANSIC       DateFormat = "Mon Jan _2 15:04:05 2006"
	UnixDate    DateFormat = "Mon Jan _2 15:04:05 MST 2006"
	RubyDate    DateFormat = "Mon Jan 02 15:04:05 -0700 2006"
	RFC822      DateFormat = "02 Jan 06 15:04 MST"
	RFC822Z     DateFormat = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
	RFC850      DateFormat = "Monday, 02-Jan-06 15:04:05 MST"
	RFC1123     DateFormat = "Mon, 02 Jan 2006 15:04:05 MST"
	RFC1123Z    DateFormat = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
	RFC3339     DateFormat = "2006-01-02T15:04:05Z07:00"
	RFC3339Nano DateFormat = "2006-01-02T15:04:05.999999999Z07:00"
	Kitchen     DateFormat = "3:04PM"
	// Handy time stamps.
	Stamp      DateFormat = "Jan _2 15:04:05"
	StampMilli DateFormat = "Jan _2 15:04:05.000"
	StampMicro DateFormat = "Jan _2 15:04:05.000000"
	StampNano  DateFormat = "Jan _2 15:04:05.000000000"
	DateTime   DateFormat = "2006-01-02 15:04:05"
	DateOnly   DateFormat = "2006-01-02"
	TimeOnly   DateFormat = "15:04:05"
)

type LogStatus

type LogStatus string
const (
	StatusInfo    LogStatus = "info"
	StatusWarning LogStatus = "warning"
	StatusError   LogStatus = "error"
	StatusDebug   LogStatus = "debug"
	StatusNotice  LogStatus = "notice"
	StatusUnknown LogStatus = "unknown"
)

Constants representing different log levels.

type Options

type Options func(l *ToLog)

Options is a function type for specifying log options using functional options pattern.

func WithContext

func WithContext(ctx string) Options

WithContext sets the log context using functional options.

func WithType

func WithType(level LogStatus) Options

WithType sets the log type using functional options.

type ToLog

type ToLog struct {
	FullLog string
	// contains filtered or unexported fields
}

ToLog represents a log entry with various attributes.

func Debug

func Debug(ctx string) *ToLog

Debug sets the log type to "debug" and sets the log context for an existing ToLog instance.

func Debugf

func Debugf(format string, a ...any) *ToLog

Debugf sets the log type to "debug" and sets the formatted log context for an existing ToLog instance.

func Debugln

func Debugln(a ...any) *ToLog

Debugln sets the log type to "debug" and sets the log context with a newline for an existing ToLog instance.

func Error

func Error(ctx string) *ToLog

Error sets the log type to "error" and sets the log context for an existing ToLog instance.

func Errorf

func Errorf(format string, a ...any) *ToLog

Errorf sets the log type to "error" and sets the formatted log context for an existing ToLog instance.

func Errorln

func Errorln(a ...any) *ToLog

Errorln sets the log type to "error" and sets the log context with a newline for an existing ToLog instance.

func Info

func Info(ctx string) *ToLog

Info sets the log type to "info" and sets the log context for an existing ToLog instance.

func Infof

func Infof(format string, a ...any) *ToLog

Infof sets the log type to "info" and sets the formatted log context for an existing ToLog instance.

func Infoln

func Infoln(a ...any) *ToLog

Infoln sets the log type to "info" and sets the log context with a newline for an existing ToLog instance.

func Log

func Log(options ...Options) *ToLog

Log creates a new ToLog instance with default values and applies any specified options.

func Notice

func Notice(ctx string) *ToLog

Notice sets the log type to "notice" and sets the log context for an existing ToLog instance.

func Noticef

func Noticef(format string, a ...any) *ToLog

Noticef sets the log type to "notice" and sets the formatted log context for an existing ToLog instance.

func Noticeln

func Noticeln(a ...any) *ToLog

Noticeln sets the log type to "notice" and sets the log context with a newline for an existing ToLog instance.

func Warning

func Warning(ctx string) *ToLog

Warning sets the log type to "warning" and sets the log context for an existing ToLog instance.

func Warningf

func Warningf(format string, a ...any) *ToLog

Warningf sets the log type to "warning" and sets the formatted log context for an existing ToLog instance.

func Warningln

func Warningln(a ...any) *ToLog

Warningln sets the log type to "warning" and sets the log context with a newline for an existing ToLog instance.

func (*ToLog) Context

func (l *ToLog) Context(ctx string) *ToLog

Context sets the log context for an existing ToLog instance.

func (*ToLog) PrintAndWrite deprecated

func (l *ToLog) PrintAndWrite()

Deprecated: PrintAndWriteSafe instead

func (*ToLog) PrintAndWriteSafe

func (l *ToLog) PrintAndWriteSafe()

func (*ToLog) PrintLog

func (l *ToLog) PrintLog() *ToLog

PrintLog prints the full log to the console for an existing ToLog instance.

func (*ToLog) Type

func (l *ToLog) Type(le string) *ToLog

Type sets the log type for an existing ToLog instance.

func (*ToLog) Write deprecated

func (l *ToLog) Write()

Deprecated: WriteSafe instead

func (*ToLog) WriteSafe

func (l *ToLog) WriteSafe()

WriteSafe writes the full log to the log file using a concurrent channel.

Jump to

Keyboard shortcuts

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