tangra

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: MIT Imports: 8 Imported by: 3

README

tangra

Go Reference Repository License Code Size Last Commit

A lightweight and easy to use Go logging library that includes logging functionalities with different levels and custom formatting. It can also be used as a library to simply format the various messages you print in the terminal.

Showcase

macOS

macOS Showcase

Linux

Linux Showcase

Windows

Windows Showcase

Installation

If you want to use this library for one of your projects, you can install it like any other Go library

go get github.com/kkrypt0nn/tangra

Customizing

Prefix

The prefix, what comes before the message, can be changed with the SetPrefix method on a Logger structure.

The default prefix is ${datetime} ${level:color}${level:name}${reset}:

Placeholders

There are formatting placeholders that will be replaced in both the message and the prefix that can be seen here. All the placeholders in effect can be seen in the placeholders example.

For example, logging the following message

${fg:red}${effect:blink}${effect:bold}${sys:username} says hello!

Will print a red blinking message in bold that says <username> says hello!, where <username> is the username on your system.

Styling

You can choose whether you want to style your messages or not with the SetStyling method on a Logger structure. Styling includes foreground colors, background colors and special effects such as bold, and others - see the terminal package.

Note: The styling will not apply to the message if it is not supported by the terminal.

Log File

Logs can also be written inside a log file with styling removed. See the example here.

License

This library was made with 💜 by Krypton and is under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Aliases are the aliases for the following placeholders
	Aliases = map[string]string{

		"${date}":             "${now:date}",
		"${time}":             "${now:time}",
		"${datetime}":         "${now:datetime}",
		"${sys:arch}":         "${sys:architecture}",
		"${architecture}":     "${sys:architecture}",
		"${arch}":             "${sys:architecture}",
		"${sys:os}":           "${sys:operating_system}",
		"${operating_system}": "${sys:operating_system}",
		"${os}":               "${sys:operating_system}",
		"${hostname}":         "${sys:hostname}",
		"${username}":         "${sys:username}",
		"${groupid}":          "${sys:groupid}",
		"${userid}":           "${sys:userid}",
		"${function}":         "${caller:function}",
		"${shortfunction}":    "${caller:shortfunction}",
		"${file}":             "${caller:file}",
		"${line}":             "${caller:line}",

		"${black}":         "${fg:black}",
		"${red}":           "${fg:red}",
		"${green}":         "${fg:green}",
		"${yellow}":        "${fg:yellow}",
		"${blue}":          "${fg:blue}",
		"${purple}":        "${fg:purple}",
		"${cyan}":          "${fg:cyan}",
		"${white}":         "${fg:white}",
		"${gray}":          "${fg:gray}",
		"${brightred}":     "${fg:brightred}",
		"${brightgreen}":   "${fg:brightgreen}",
		"${brightyellow}":  "${fg:brightyellow}",
		"${brightblue}":    "${fg:brightblue}",
		"${brightpurple}":  "${fg:brightpurple}",
		"${brightcyan}":    "${fg:brightcyan}",
		"${brightwhite}":   "${fg:brightwhite}",
		"${bblack}":        "${bg:black}",
		"${bred}":          "${bg:red}",
		"${bgreen}":        "${bg:green}",
		"${byellow}":       "${bg:yellow}",
		"${bblue}":         "${bg:blue}",
		"${bpurple}":       "${bg:purple}",
		"${bcyan}":         "${bg:cyan}",
		"${bwhite}":        "${bg:white}",
		"${bgray}":         "${bg:gray}",
		"${bbrightred}":    "${bg:brightred}",
		"${bbrightgreen}":  "${bg:brightgreen}",
		"${bbrightyellow}": "${bg:brightyellow}",
		"${bbrightblue}":   "${bg:brightblue}",
		"${bbrightpurple}": "${bg:brightpurple}",
		"${bbrightcyan}":   "${bg:brightcyan}",
		"${bbrightwhite}":  "${bg:brightwhite}",
		"${bold}":          "${effect:bold}",
		"${dim}":           "${effect:dim}",
		"${underline}":     "${effect:underline}",
		"${blink}":         "${effect:blink}",
		"${inverse}":       "${effect:inverse}",
		"${strikethrough}": "${effect:strikethrough}",
		"${reset}":         "${effect:reset}",
	}

	// Variables are the different variables to replace.
	Variables = map[string]func() string{

		"${caller:function}": func() string {
			pc, _, _, ok := runtime.Caller(4)
			details := runtime.FuncForPC(pc)
			if ok && details != nil {
				return details.Name()
			}
			return ""
		},
		"${caller:shortfunction}": func() string {
			pc, _, _, ok := runtime.Caller(4)
			details := runtime.FuncForPC(pc)
			split := strings.Split(details.Name(), ".")
			if ok && details != nil && len(split) >= 2 {
				return split[len(split)-1]
			}
			return ""
		},
		"${caller:file}": func() string {
			_, file, _, ok := runtime.Caller(3)
			split := strings.Split(file, "/")
			if ok && len(split) >= 1 {
				return split[len(split)-1]
			}
			return ""
		},
		"${caller:line}": func() string {
			_, _, line, ok := runtime.Caller(3)
			if ok {
				return strconv.Itoa(line)
			}
			return "0"
		},

		"${level:color}": func() string {
			return GetLevelColor(CurrentLoggingLevel)
		},
		"${level:lowername}": func() string {
			return strings.ToLower(GetLevelName(CurrentLoggingLevel))
		},
		"${level:name}": func() string {
			return GetLevelName(CurrentLoggingLevel)
		},
		"${level:shortname}": func() string {
			return GetLevelShortName(CurrentLoggingLevel)
		},

		"${now:date}": func() string {
			return time.Now().Format(CurrentDateFormat)
		},
		"${now:time}": func() string {
			return time.Now().Format(CurrentTimeFormat)
		},
		"${now:datetime}": func() string {
			return time.Now().Format(CurrentDatetimeFormat)
		},

		"${sys:architecture}": func() string {
			return runtime.GOARCH
		},
		"${sys:hostname}": func() string {
			hostname, err := os.Hostname()
			if err != nil {
				return ""
			}
			return hostname
		},
		"${sys:operating_system}": func() string {
			return runtime.GOOS
		},
		"${sys:username}": func() string {
			user, err := user.Current()
			if err != nil {
				return ""
			}
			return user.Username
		},
		"${sys:groupid}": func() string {
			user, err := user.Current()
			if err != nil {
				return ""
			}
			return user.Gid
		},
		"${sys:userid}": func() string {
			user, err := user.Current()
			if err != nil {
				return ""
			}
			return user.Uid
		},
	}
	// Styles are the different types of styling.
	Styles = map[string]string{

		"${fg:black}":        terminal.BLACK,
		"${fg:red}":          terminal.RED,
		"${fg:green}":        terminal.GREEN,
		"${fg:yellow}":       terminal.YELLOW,
		"${fg:blue}":         terminal.BLUE,
		"${fg:purple}":       terminal.PURPLE,
		"${fg:cyan}":         terminal.CYAN,
		"${fg:white}":        terminal.WHITE,
		"${fg:gray}":         terminal.GRAY,
		"${fg:brightred}":    terminal.BRIGHT_RED,
		"${fg:brightgreen}":  terminal.BRIGHT_GREEN,
		"${fg:brightyellow}": terminal.BRIGHT_YELLOW,
		"${fg:brightblue}":   terminal.BRIGHT_BLUE,
		"${fg:brightpurple}": terminal.BRIGHT_PURPLE,
		"${fg:brightcyan}":   terminal.BRIGHT_CYAN,
		"${fg:brightwhite}":  terminal.BRIGHT_WHITE,

		"${bg:black}":        terminal.BG_BLACK,
		"${bg:red}":          terminal.BG_RED,
		"${bg:green}":        terminal.BG_GREEN,
		"${bg:yellow}":       terminal.BG_YELLOW,
		"${bg:blue}":         terminal.BG_BLUE,
		"${bg:purple}":       terminal.BG_PURPLE,
		"${bg:cyan}":         terminal.BG_CYAN,
		"${bg:white}":        terminal.BG_WHITE,
		"${bg:gray}":         terminal.BG_GRAY,
		"${bg:brightred}":    terminal.BG_BRIGHT_RED,
		"${bg:brightgreen}":  terminal.BG_BRIGHT_GREEN,
		"${bg:brightyellow}": terminal.BG_BRIGHT_YELLOW,
		"${bg:brightblue}":   terminal.BG_BRIGHT_BLUE,
		"${bg:brightpurple}": terminal.BG_BRIGHT_PURPLE,
		"${bg:brightcyan}":   terminal.BG_BRIGHT_CYAN,
		"${bg:brightwhite}":  terminal.BG_BRIGHT_WHITE,

		"${effect:bold}":          terminal.BOLD,
		"${effect:dim}":           terminal.DIM,
		"${effect:underline}":     terminal.UNDERLINE,
		"${effect:blink}":         terminal.BLINK,
		"${effect:inverse}":       terminal.INVERSE,
		"${effect:strikethrough}": terminal.STRIKETHROUGH,
		"${effect:reset}":         terminal.RESET,
	}
)
View Source
var (
	// LevelColors convert a logging level to its color.
	LevelColors = map[Level]string{
		TRACE: terminal.GRAY,
		DEBUG: terminal.GRAY + terminal.BOLD,
		INFO:  terminal.BLUE + terminal.BOLD,
		WARN:  terminal.YELLOW + terminal.BOLD,
		ERROR: terminal.RED + terminal.BOLD,
		FATAL: terminal.BRIGHT_WHITE + terminal.BOLD + terminal.BG_RED,
	}
	// LevelNames convert a logging level to its name.
	LevelNames = map[Level]string{
		TRACE: "TRACE",
		DEBUG: "DEBUG",
		INFO:  "INFO",
		WARN:  "WARN",
		ERROR: "ERROR",
		FATAL: "FATAL",
	}
	// ShortLevelNames convert a logging level to its short name.
	ShortLevelNames = map[Level]string{
		TRACE: "tra",
		DEBUG: "dbg",
		INFO:  "inf",
		WARN:  "war",
		ERROR: "err",
		FATAL: "fat",
	}
)
View Source
var (
	CurrentLoggingLevel   = NONE
	CurrentDateFormat     = "Jan 02, 2006"
	CurrentDatetimeFormat = "Jan 02, 2006 15:04:05"
	CurrentTimeFormat     = "15:04:05"

	ForceStyling = false
)

Functions

func AddStyling

func AddStyling(message string) string

AddStyling will add styling into the message.

func AddVariables

func AddVariables(message string) string

AddVariables will add the various variables into the message.

func GetLevelColor

func GetLevelColor(id Level) string

GetLevelColor returns the color of the level.

func GetLevelName

func GetLevelName(id Level) string

GetLevelName returns the name of the level.

func GetLevelShortName

func GetLevelShortName(id Level) string

GetLevelShortName returns the short name of the level.

func RemoveStyling

func RemoveStyling(message string) string

RemoveStyling will remove styling from the message.

func ReplaceAliases

func ReplaceAliases(message string) string

ReplaceAliases will replace the aliases with the original placeholder.

Types

type Level

type Level int

Level represents the level of logging - https://logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/Level.html

const (
	// TRACE level designates finer-grained informational events than the DEBUG.
	TRACE Level = iota
	// DEBUG level designates fine-grained informational events that are most useful to debug an application.
	DEBUG
	// INFO level designates informational messages that highlight the progress of the application at coarse-grained level.
	INFO
	// WARN level designates potentially harmful situations.
	WARN
	// ERROR level designates error events that might still allow the application to continue running.
	ERROR
	// FATAL level designates very severe error events that will presumably lead the application to abort.
	FATAL
	// NONE will reset the logging level to nothing.
	NONE Level = 1337
)

type Logger

type Logger struct {
	// Styling describes whether the logger should style the logged message.
	Styling bool
	// Prefix is the prefix before the logged message.
	Prefix string
	// LogFile is the log file to write the messages into.
	LogFile *os.File
}

Logger is represents a logger structure.

func NewLogger

func NewLogger() *Logger

NewLogger creates a new logger.

func (*Logger) Debug

func (l *Logger) Debug(message string)

Debug prints a debug message.

func (*Logger) Error

func (l *Logger) Error(message string)

Error prints an error message.

func (*Logger) Fatal

func (l *Logger) Fatal(message string)

Fatal prints a fatal error message.

func (*Logger) Info

func (l *Logger) Info(message string)

Info prints an info message.

func (*Logger) Log

func (l *Logger) Log(level Level, message string)

Log will log with the given level.

func (*Logger) Print

func (l *Logger) Print(message string)

Print simply prints the message, without logging level.

func (*Logger) Println

func (l *Logger) Println(message string)

Println simply prints the message with a new line, without logging level.

func (*Logger) SetDateFormat

func (l *Logger) SetDateFormat(format string)

SetDateFormat sets the format to use when logging the date.

func (*Logger) SetDatetimeFormat

func (l *Logger) SetDatetimeFormat(format string)

SetDatetimeFormat sets the format to use when logging the date and time.

func (*Logger) SetForceStyling

func (l *Logger) SetForceStyling(forceStyling bool)

SetForceStyling sets to whether it should force the styling render.

func (*Logger) SetLevelColor

func (l *Logger) SetLevelColor(id Level, color string)

SetLevelColor sets the color of the level.

func (*Logger) SetLogFile

func (l *Logger) SetLogFile(file *os.File)

SetLogFile will set the log file to write logs into.

func (*Logger) SetLoggingLevel

func (l *Logger) SetLoggingLevel(level Level)

SetLoggingLevel sets the logging level.

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(prefix string)

SetPrefix sets the prefix before the log message.

func (*Logger) SetStyling

func (l *Logger) SetStyling(styling bool)

SetStyling sets the styling setting of the logger.

func (*Logger) SetTimeFormat

func (l *Logger) SetTimeFormat(format string)

SetTimeFormat sets the format to use when logging the time.

func (*Logger) Trace

func (l *Logger) Trace(message string)

Trace prints a tracing message.

func (*Logger) Warn

func (l *Logger) Warn(message string)

Warn prints a warning message.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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