util

package module
v0.0.0-...-7e18dac Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package util contains common functionality which is required by a typical server side any GO application. Namely it contains functionality to read JSON configuration file (from ilyakaznacheev) and rotating logging functionality (from lumberjack). Essentially these are few simple useful wrappers convenient for any GO application.

If user passes a configuration file with absolute path, it is read as is. Otherwise environment variable GO_CFG_HOME is checked to see if contains the directory name in which the passed configuration file is checked. If found, it is read and configuration structure is returned.

There are three methods to initialize logs and set log settings:

1) Caller can pass LoggingCfg structure as defined here to initialize logs.

2) Alternatively, assuming caller has used 'cfg' program in the 'util' package, that 'uber' configuration file can be passed as long as it has a JSON member of name "LogSettings". The configuration file can be passed: - either with the absolute path - or the file name only in which case GP_CFG_HOME environment variable value is used as the directory name where the give configuration file is located.

3) Finally, caller can explicitly call InitializeLog method with arguments. The file path can be absolute. It is not absolute, it is appended to the current working directory.

Until LoggingCfg is set, GlobalLogSettings will be nil. All log calls will be handled by the built in logging facility of Golang. Once the logging is initialized by one of the above mentioned 3 methods; log calls will be directing the output to the specified rotating log file.

Index

Constants

View Source
const LoggingCfgJsonElementName = "LogSettings"

Name of the Json element in any Json Configuration file which contains LoggingCfg structure value.

Variables

This section is empty.

Functions

func ExtractCfgJsonEleFromBytes

func ExtractCfgJsonEleFromBytes(byteValue []byte, cfgJsonElementName string) ([]byte, error)

Returns the byte array corresponding to json segment of the specified config element in the passed byte array. Second argument is the name of the configuration structure member caller is seeking.

func ExtractCfgJsonEleFromFile

func ExtractCfgJsonEleFromFile(fileName string, cfgJsonElementName string) ([]byte, error)

Returns the byte array corresponding to json segment of specified config element in the passed filename. If file name is with the absolute path, reading is attempted for that location, else the given file name is looked up in to the directory as pointed by GO_CFG_HOME. Second argument is the name of the configuration structure member caller is seeking.

func GetCfgHomeDir

func GetCfgHomeDir() (string, error)

Returns directory which holds config files.

func InitializeLog

func InitializeLog(fn string, ms int, bk int, age int, compress bool)

Initialize logging to given inputs: fn : Log files with fill path ms : Maximum allowed log file size in Megabytes bk : How many backups to be retained. age : Past logs of how many days to be retained. compress: Whether logs are compressed or not.

func IsLoggingConfigured

func IsLoggingConfigured() bool

Indicates whether logging has been configured or not.

func Log

func Log(msg string)

Log the given message.

func LogDebug

func LogDebug(msg string)

Log debug messages. Invocation of this call will result in adding the message to the log provided SetDebugLog(true) is called.

func ReadCfg

func ReadCfg(cfg interface{}, fileName string) error

Read configurations

func SetConsoleLog

func SetConsoleLog(val bool)

Enable or disable the console logging

func SetDeubgLog

func SetDeubgLog(val bool)

Enable or disable debug logging.

func SetLogSettings

func SetLogSettings(cfgFileName string)

It assumes argument configuration file in JSON format and it contains an element named LogSettings. Contents of that member are used to build the log setting configuration.

func SetLoggingCfg

func SetLoggingCfg(ls *LoggingCfg)

Types

type ConfigHome

type ConfigHome struct {
	Dir string `env:"GO_CFG_HOME" env-description:"Directory where we can find configuration file"`
}

type LoggingCfg

type LoggingCfg struct {
	LogFileName  string
	MaxSizeInMb  int
	Backups      int
	AgeInDays    int
	Compress     bool
	LogOnConsole bool
	DebugLog     bool
}
var GlobalLogSettings *LoggingCfg

Pointer to a structure which holds log settings in effect.

func FormLoggingCfg

func FormLoggingCfg(ls []byte) (*LoggingCfg, error)

Make logging configuration struct from the given input string.

Jump to

Keyboard shortcuts

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