# README
Typewriter
This repository provides a GoLang logging package, which are inspired by some Log4j features like package specific enablement
:warning: :construction: Unstable release v0.0.2 (See also Module version numbering)
Nevertheless, pre-release versions can already be tested: typewriter at pkg.go.dev
Sonar analysis
Usage
Logging
Access
The logger can be accessed via importing github.com/ma-vin/typewriter/logger
and using the Log()
method at logger
package. This will return a struct implementing the Logger
interface.
There are also the same methods directly in the logger
package, but without returning an interface.
Log Methods
There are permutations of the following categories for each severity
- log without additional parameters, e.g.
Information(args ...any)
- log with correlation, e.g.
InformationWithCorrelation(correlationId string, args ...any)
- log with custom values map, e.g.
InformationCustom(customValues map[string]any, args ...any)
- message format with args, e.g.
Informationf(format string, args ...any)
- panic (only for warning, error and fatal) e.g.
ErrorWithPanic(args ...any)
- exit (only for fatal) e.g.
FatalWithExit(args ...any)
Arguments are handled in the manner of fmt.Sprint
/fmt.Sprintf
In addition, there exists an indicator if the severity is enabled or not.
Configuration
Configuration is currently possible via environment variables or an properties file.
Configauration by file
To use a configuration file by setting an environment variable TYPEWRITER_CONFIG_FILE
with a path to a properties file.
If this environment variable is set, all others will be ignored. Nevertheless, the same property names are used by both.
It is possible to use single line comments #
,//
or --
and multiline commets with begin at /*
and end at */
.
Log levels
The log level can be set via TYPEWRITER_LOG_LEVEL
with one of the following values:
DEBUG
INFO
orINFORMATION
WARN
orWARNING
ERROR
FATAL
The default log level is ERROR
Appender
There are two types of appender at the moment. They can be configured by setting TYPEWRITER_LOG_APPENDER_TYPE
with one of the following values:
STDOUT
: Standard output appenderFILE
: File appender which writes to a file- The target file of the appender has to be configured by setting the path to
TYPEWRITER_LOG_APPENDER_PARAMETER
- The target file of the appender has to be configured by setting the path to
The default appender is the standard output one
Formatter
There are three types of formatter which provides the texts to log for the appender. They can be configured by setting TYPEWRITER_LOG_FORMATTER_TYPE
with one of the following values:
-
DELIMITER
: The record information will be appended and delimited with a given sign. They can be setTYPEWRITER_LOG_FORMATTER_PARAMETER_<x>
where<x>
has to be replaced by the following values:DELIMITER
the delimiting signs. The default value of the delimiter is-
.TIME_LAYOUT
time layout. Default value oftime.RFC3339
-
TEMPLATE
: The records will be derived from three templates and time layout. They can be setTYPEWRITER_LOG_FORMATTER_PARAMETER_<x>
where<x>
has to be replaced by the following values:TEMPLATE
template for writing time, severity and the message. Default[%s] %s: %s
TEMPLATE_CORRELATION
template for writing time, severity, correlationID and the message. Default[%s] %s %s: %s
TEMPLATE_CUSTOM
template for writing time, severity, message and custom value map. Default[%s] %s: %s
TIME_LAYOUT
time layout. Default value oftime.RFC3339
TEMPLATE_TRIM_SEVERITY
indicator whether to trim severity text or to add space at warn and info to algin following elements. Defaultfalse
TEMPLATE_CALLER
like 1. with caller function, file and line placed in front of message. Default[%s] %s %s(%s.%d): %s
TEMPLATE_CALLER_CORRELATION
like 2. with caller function, file and line placed in front of message. Default[%s] %s %s %s(%s.%d): %s
TEMPLATEE_CALLER_CUSTOM
like 3. with caller function, file and line placed in front of message. Default[%s] %s %s(%s.%d): %s
It is possible to reorder parameter by argument indices. The custom value map at 3 will be appended as key-value-pairs sorted by key (e.g. a custom map with three entries of string, number and boolean format at indices from 4 to 9:
severity: %[2]s message: %[3]s %[4]s: %[5]s %[6]s: %[7]d %[8]s: %[9]t time: %[1]s
) -
JSON
: The records will be logged as JSON. It is possible to define key names, the time layout and if the custom value map should be a sub element or not. The keys of the custom value map will be used 1:1. The properties can be set viaTYPEWRITER_LOG_FORMATTER_PARAMETER_<x>
where<x>
has to be replaced by the following values:JSON_TIME_KEY
key of time. Defaulttime
JSON_SEVERITY_KEY
key of severity. Default:severity
JSON_CORRELATION_KEY
key of correlationID. Default:correlation
JSON_MESSAGE_KEY
key of message. Default:message
JSON_CUSTOM_VALUES_KEY
key of custom values if used as sub elements. Default:custom
JSON_CUSTOM_VALUES_SUB
indicator to add custom value map as sub element: Default:false
TIME_LAYOUT
time layout. Default value oftime.RFC3339
JSON_CALLER_FUNCTIOM_KEY
key of the caller function. Default:caller
JSON_CALLER_FILE_KEY
key of the caller file. Default:file
JSON_CALLER_LINE_KEY
key of the caller file line. Default:line
The default formatter is the delimiter one.
Caller
The logging of the caller function, file and line can be activated by setting true
at TYPEWRITER_LOG_CALLER
. The default is false
.
Package specific configuration
Each configuration element can be declared package specific by replacing TYPEWRITER
with TYPEWRITER_PACKAGE
and adding the package as postfix at environment variable names.
The table shows the corresponding variable names for the example package logger
general | package specific |
---|---|
TYPEWRITER_LOG_LEVEL | TYPEWRITER_PACKAGE_LOG_LEVEL_LOGGER |
TYPEWRITER_LOG_APPENDER_TYPE | TYPEWRITER_PACKAGE_LOG_APPENDER_TYPE_LOGGER |
TYPEWRITER_LOG_APPENDER_PARAMETER | TYPEWRITER_PACKAGE_LOG_APPENDER_PARAMETER_LOGGER |
TYPEWRITER_LOG_FORMATTER_TYPE | TYPEWRITER_PACKAGE_LOG_FORMATTER_TYPE_LOGGER |
TYPEWRITER_LOG_FORMATTER_PARAMETER | TYPEWRITER_PACKAGE_LOG_FORMATTER_PARAMETER_LOGGER |
TYPEWRITER_LOG_FORMATTER_PARAMETER_DELIMITER | TYPEWRITER_PACKAGE_LOG_FORMATTER_PARAMETER_LOGGER_DELIMITER |
Examples
Examples are avialable at godoc logger/example_test.go