Skip to main content

Configure Logging

In order to configure the logging, you can use the following approaches:

  • Environment Variables
  • Programmatically

You can load the yaml logging configuration files as shown below.

func init() {
err := logy.LoadConfigFromYaml("logy.config.yaml")

if err != nil {
panic(err)
}
}

As an alternative, you can configure the logging by invoking logy.LoadConfig() function.

func init() {
err := logy.LoadConfig(&logy.Config{
Level: logy.LevelTrace,
Handlers: logy.Handlers{"console", "file"},
Console: &logy.ConsoleConfig{
Level: logy.LevelTrace,
Enabled: true,
// this will be ignored because console json logging is enabled
Format: "%d{2006-01-02 15:04:05.000} %l [%x{traceId},%x{spanId}] %p : %s%e%n",
Color: true,
Json: &logy.JsonConfig{
Enabled: true,
KeyOverrides: logy.KeyOverrides{
"timestamp": "@timestamp",
},
AdditionalFields: logy.JsonAdditionalFields{
"application-name": "my-logy-app",
},
},
},
File: &logy.FileConfig{
Enabled: true,
Name: "file_trace.log",
Path: "/var",
// this will be ignored because file json logging is enabled
Format: "d{2006-01-02 15:04:05} %p %s%e%n",
Json: &logy.JsonConfig{
Enabled: true,
KeyOverrides: logy.KeyOverrides{
"timestamp": "@timestamp",
},
AdditionalFields: logy.JsonAdditionalFields{
"application-name": "my-logy-app",
},
},
},
})

if err != nil {
panic(err)
}

}

Logging Package

Logging is done on a per-package basis. Each package can be independently configured. A configuration which applies to a package will also apply to all sub-categories of that package, unless there is a more specific matching sub-package configuration.

For every package the same settings that are configured on ( console / file / syslog ) apply.

PropertyDescriptionTypeDefault
logy.package.package-path.levelThe log level for this packageboolTRACE
logy.package.package-path.use-parent-handlersSpecify whether this logger should user its parent handlersbooltrue
logy.package.package-path.handlersThe names of the handlers to link to this packagelist of string

Root Logger Configuration

The root logger is handled separately, and is configured via the following properties:

PropertyDescriptionTypeDefault
logy.levelThe log level for every log packageboolTRACE
logy.handlersThe names of handlers to link to the rootlist of string[console]

Logging Format

By default, Logy uses a pattern-based logging format.

You can customize the format for each log handler using a dedicated configuration property. For the console handler, the property is logy.console.format.

The following table shows the logging format string symbols that you can use to configure the format of the log messages.

Supported logging format symbols:

SymbolSummaryDescription
%%%A simple %%character
%cLogger nameThe logger name
%CPackage nameThe package name
%d{layout}DateDate with the given layout string
%eErrorThe error stack trace
%FSource fileThe source file name
%iProcess IDThe current process PID
%lSource locationThe source location(file name, line number, method name)
%LSource lineThe source line number
%mFull MessageThe log message including error trace
%MSource methodThe source method name
%nNewlineThe line separator string
%NProcess nameThe name of the current process
%pLevelThe logging level of the message
%sSimple messageThe log message without error trace
%X{property-name}Mapped Context ValueThe value from Mapped Context property-key=property-value
%x{property-name}Mapped Context Value without keyThe value without key from Mapped Context in format property-value
%XMapped Context ValuesAll the values from Mapped Context in format property-key1=property-value1,property-key2=property-value2
%xMapped Context Values without keysAll the values without keys from Mapped Context in format property-value1,property-value2

Console Handler Properties

You can configure the console handler with the following configuration properties:

PropertyDescriptionTypeDefault
logy.console.enabledEnable the console loggingbooltrue
logy.console.targetOverride keys with custom valuesTarget(stdout, stderr, discard)stdout
logy.console.formatThe console log format. Note that this value will be ignored if json is enabled for consolestringd{2006-01-02 15:04:05.000000} %p %c : %m%n
logy.console.colorEnable color coded output if the target terminal supports itbooltrue
logy.console.levelThe console log levelLevel(OFF,ERROR,WARN,INFO,DEBUG,TRACE,ALL)TRACE
logy.console.json.enabledEnable the JSON console formattingboolfalse
logy.console.json.excluded-keysKeys to be excluded from the Json outputlist of string
logy.console.json.key-overrides.property-nameOverride keys with custom valuesmap[string]string
logy.console.json.additional-fields.property-nameAdditional field valuesmap[string]any

File Handler Properties

You can configure the file handler with the following configuration properties:

PropertyDescriptionTypeDefault
logy.file.enabledEnable the file loggingboolfalse
logy.file.formatThe file log format. Note that this value will be ignored if json is enabled for filestringd{2006-01-02 15:04:05.000000} %p %c : %m%n
logy.file.nameThe name of the file in which logs will be writtenstringlogy.log
logy.file.pathThe path of the file in which logs will be writtenstringWorking directory
logy.file.levelThe level of logs to be written into the fileLevel(OFF,ERROR,WARN,INFO,DEBUG,TRACE,ALL)TRACE
logy.file.json.enabledEnable the JSON file formattingboolfalse
logy.file.json.excluded-keysKeys to be excluded from the Json outputlist of string
logy.file.json.key-overrides.property-nameOverride keys with custom valuesmap[string]string
logy.file.json.additional-fields.property-nameAdditional field valuesmap[string]any

Syslog Handler Properties

You can configure the syslog handler with the following configuration properties:

PropertyDescriptionTypeDefault
logy.syslog.enabledEnable the syslog loggingboolfalse
logy.syslog.endpointThe IP address and port of the syslog serverhost:portlocalhost:514
logy.syslog.app-nameThe app name used when formatting the message in RFC5424 formatstring
logy.syslog.hostnameThe name of the host the messages are being sent fromstring
logy.syslog.facilityThe facility used when calculating the priority of the message in RFC5424 and RFC3164 formatFacility(kernel,user-level,mail-system,system-daemons,security,syslogd,line-printer,network-news,uucp,clock-daemon,security2,ftp-daemon,ntp,log-audit,log-alert,clock-daemon2,local-use-0,local-use-1,local-use-2,local-use-3,local-use-4,local-use-5,local-use-6,local-use-7user-level
logy.syslog.log-typeThe message format type used when formatting the messageSysLogType(rfc5424,rfc3164)rfc5424
logy.syslog.protocolThe protocol used to connect to the syslog serverProtocol(tcp,udp)tcp
logy.syslog.block-on-reconnectEnable or disable blocking when attempting to reconnect the syslog serverboolfalse
logy.syslog.formatThe log message formatstringd{2006-01-02 15:04:05.000000} %p %c : %m%n
logy.syslog.levelThe level of the logs to be logged by syslog loggerLevel(OFF,ERROR,WARN,INFO,DEBUG,TRACE,ALL)TRACE