hlog

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2021 License: MIT Imports: 9 Imported by: 0

README

hlog package

This package is only used for golang beginners' practice. I cannot guarantee its quality.
This package provides a brief logging module.

How to use

After importing, use hlog.Init() to specify where the log file store and some relative information. defer hlog.Finalize() ensure all logs will be recorded.

//init log.
hlog.Init("./logFileDir/", "logFileNamePrefix", "logInFilePrefix", hlog.AutoScreenFile)
defer hlog.Finalize()

To add a new log, use hlog.Info(), hlog.Error() and more. For instance:

hlog.Info("Listening on " + CONN_HOST + ":" + CONN_PORT + "\n")   // here~
l, err := net.Listen(CONN_TYPE, ":"+CONN_PORT)
if err != nil {
  hlog.Panic("Error listening: %s", err.Error())                  // and here!
}                                                                 // Note that hlog.Panic() will throw a panic while hlog.Error() will not.
defer l.Close()

Initialization

Use hlog.Init(logFileDir, logFileNamePrefix, logInFilePrefix string, args ...interface{}) to initialize.
You must specify logFileDir, logFileNamePrefix and logInFilePrefix before starting log.

  • logFileDir is the path where the log file is stored. It can be with or without a suffix /. If you provide a invalid path, it will use "./.logfile/" as default path.
  • logFileNamePrefix is the file name prefix of your log, which is in the logFileDir directory.
  • logInFilePrefix is the prefix of each of your logs.

Suppose you used hlog.Init("./.myLogDir", "myFile.r", "myLog") and added some log using hlog.Info(), you will have a file named ./.myLogDir/myFile.r, in where contains some log like:

myLog [INF] 2021-07-23 09:00:00.00 -> source_file.go:21 (pc=a9644b): your commits...
myLog [EMP] 2021-07-23 09:01:00.00 -> source_file.go:893 (pc=a869a5): your commits...
myLog [INF] 2021-07-23 09:02:00.00 -> source_file.go:47 (pc=a96ecd): your commits...
myLog [INF] 2021-07-23 09:03:00.00 -> source_file.go:49 (pc=d2644b): your commits...
myLog [INF] 2021-07-23 09:04:00.00 -> source_file.go:53 (pc=10c5bb3): your commits...
myLog [INF] 2021-07-23 09:05:00.00 -> source_file.go:78 (pc=10d0853): your commits...
myLog [WAN] 2021-07-23 09:06:00.00 -> source_file.go:976 (pc=506989): your commits...

Some optional parameters are available. For instance, you can decide where the log output goes. Use AutoScreenFile, AutoScreen, AllStdOutScreenFile, AllStdOutScreen or OnlyFile in hlog.Init().

  • AutoScreenFile log at stdout and stderr (depends on the log category) and write to file.
  • AutoScreen log at stdout and stderr (depends on the log category) but not write to file.
  • AllStdOutScreenFile log at stdout (whatever the log category) and write to file.
  • AllStdOutScreen log at stdout (whatever the log category) but not write to file.
  • OnlyFile write to file but not print at screen (higher logging speed).

You can also use an int variable to specify calldepth in runtime.Caller().
Split file into small pieces to avoid large single file is support. To limit the number of logs in a single file, append Slice(num) as the optional parameter in Init(), where num is an uint indicating how many logs a single file has.

Add logs

It's easy to add logs. Using Emphasize(), Info(), Debug(), Warn(), Error(), Panic() and Fatal(). Note that Panic() will throw a panic, and Fatal() will end the process.

Colorful Terminal

This package also provides some free functions that enable you print colorful text in your POSIX-like terminal using printf-style. Now support printing default color, green, red and blue text on stdout and stderr (and I think it is sufficient for most uses).
To print, use PrintDefault(), PrintGreen(), PrintRed() and PrintBlue() like using a fmt.Printf(). Add a suffix E to print on stderr.
StartGreen(), StartRed() and StartBlue() will change the color of text printing after the function call. Use EndColor() to restore default color of text.

Documentation

Index

Constants

View Source
const (
	// Used in func hlog.Init as optional argument. Log at stdout and stderr (depends on the log category) and write to file.
	AutoScreenFile oStreamFlagType = iota
	// Used in func hlog.Init as optional argument. Log at stdout and stderr (depends on the log category) but not write to file.
	AutoScreen
	// Used in func hlog.Init as optional argument. Log at stdout (whatever the log category) and write to file.
	AllStdOutScreenFile
	// Used in func hlog.Init as optional argument. Log at stdout (whatever the log category) but not write to file.
	AllStdOutScreen
	// Used in func hlog.Init as optional argument. Write to file but not print at screen (higher logging speed).
	OnlyFile
)

Variables

This section is empty.

Functions

func Debug

func Debug(commit string, args ...interface{})

Debug prints a log in blue color with [DBG] prefix.

Init hlog first is necessary.

func Emphasize

func Emphasize(commit string, args ...interface{})

Emphasize prints a log in green color with [EMP] prefix.

Init hlog first is necessary.

func EndColor added in v0.1.2

func EndColor()

Text color will restore to default after calling this func.

func Error

func Error(commit string, args ...interface{})

Error prints a log in red color with [ERR] prefix.

Init hlog first is necessary.

func Fatal

func Fatal(commit string, exitStatusCode int)

Fatal prints a log in red color with [FAT] prefix and exit. Exit status code must be specified.

Note exit status code is an uint8 in most POSIX-like system, not int32 or uint32. Conventionally, code zero indicates success, non-zero an error. For portability, the status code should be in the range [0, 125].

Init hlog first is necessary.

func Finalize

func Finalize()

Finalize ensure all logs will be recorded and written to file (if necessary).

It is recommended to use it followed by Init in a defer statement.

func Info

func Info(commit string, args ...interface{})

Info prints a log in default color with [INF] prefix.

Init hlog first is necessary.

func Init

func Init(logFileDir, logFileNamePrefix, logInFilePrefix string, args ...interface{})

Initialize hlog.

Call it before adding any logs.

func Panic

func Panic(commit string, args ...interface{})

Panic prints a log in red color with [PAN] prefix and throw a panic.

Init hlog first is necessary.

func Pause

func Pause()

Pause pauses logs recording.

func PrintBlue added in v0.1.1

func PrintBlue(s string, v ...interface{})

Print formated string in blue color on stdout using printf-style.

func PrintBlueE

func PrintBlueE(s string, v ...interface{})

Print formated string in blue color on stderr using printf-style.

func PrintDefault

func PrintDefault(s string, v ...interface{})

Print formated string in default color on stdout using printf-style.

func PrintDefaultE

func PrintDefaultE(s string, v ...interface{})

Print formated string in default color on stderr using printf-style.

func PrintGreen

func PrintGreen(s string, v ...interface{})

Print formated string in green color on stdout using printf-style.

func PrintGreenE

func PrintGreenE(s string, v ...interface{})

Print formated string in green color on stderr using printf-style.

func PrintRed

func PrintRed(s string, v ...interface{})

Print formated string in red color on stdout using printf-style.

func PrintRedE

func PrintRedE(s string, v ...interface{})

Print formated string in red color on stderr using printf-style.

func Report added in v0.1.4

func Report()

func SetCallerDepth added in v0.1.1

func SetCallerDepth(callDepth int)

Which func and file line to print in func call stack.

func SetPrintAbsolutePath added in v0.1.1

func SetPrintAbsolutePath(status bool)

Print absolute file path or not in a log. False by default.

func Start

func Start()

To restart logging after Pause called.

func StartBlue added in v0.1.2

func StartBlue()

Text color will be blue after calling this func.

func StartGreen added in v0.1.2

func StartGreen()

Text color will be green after calling this func.

func StartRed added in v0.1.2

func StartRed()

Text color will be red after calling this func.

func Warn

func Warn(commit string, args ...interface{})

Warn prints a log in red color with [WAN] prefix.

Init hlog first is necessary.

Types

type Slice added in v0.1.5

type Slice uint

To specify how many logs a log file could contains.

Jump to

Keyboard shortcuts

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