The Go code of logFiles.go will explain the use of the log and log/syslog packages to write to the system log files.
Please note that the log/syslog package is not implemented on the Microsoft Windows version of Go.
The first part of logFiles.go is as follows:
package main
import (
"fmt"
"log"
"log/syslog"
"os"
"path/filepath"
)
func main() {
programName := filepath.Base(os.Args[0])
sysLog, err := syslog.New(syslog.LOG_INFO|syslog.LOG_LOCAL7,
programName)
The first parameter to the syslog.New() function is the priority, which is a combination of the logging facility and the logging level. Therefore, a priority of LOG_NOTICE | LOG_MAIL, which is mentioned as an example, will send notice logging level messages to the MAIL logging facility.
As a result, the preceding code sets the default logging to the local7 logging facility using the info logging level. The second parameter to the syslog.New() function is the name of the process that will appear on the logs as the sender of the message. Generally speaking, it is considered a good practice to use the real name of the executable in order to be able to easily find the information you want in the log files at another time.
The second part of the program contains the following Go code:
if err != nil {
log.Fatal(err)
} else {
log.SetOutput(sysLog)
}
log.Println("LOG_INFO + LOG_LOCAL7: Logging in Go!")
After the call to syslog.New(), you will have to check the error variable that is returned from it so that you can make sure that everything is fine. If everything is OK, which means that the value of the error variable is equal to nil, you call the log.SetOutput() function, which sets the output destination of the default logger, which, in this case, is the logger you created earlier on (sysLog). Then, you can use log.Println() to send information to the log server.
The third part of logFiles.go comes with the following code:
sysLog, err = syslog.New(syslog.LOG_MAIL, "Some program!")
if err != nil {
log.Fatal(err)
} else {
log.SetOutput(sysLog)
}
log.Println("LOG_MAIL: Logging in Go!")
fmt.Println("Will you see this?")
}
The last part shows that you can change the logging configuration in your programs as many times as you want and that you can still use fmt.Println() for printing output on the screen.
The execution of logFiles.go will create the following output on the screen of a Debian Linux machine:
$ go run logFiles.go
Broadcast message from systemd-journald@mail (Tue 2017-10-17 20:06:08 EEST):
logFiles[23688]: Some program![23688]: 2017/10/17 20:06:08 LOG_MAIL: Logging in Go!
Message from syslogd@mail at Oct 17 20:06:08 ...
Some program![23688]: 2017/10/17 20:06:08 LOG_MAIL: Logging in Go!
Will you see this?
Executing the same Go code on a macOS High Sierra machine generated the following output:
$ go run logFiles.go
Will you see this?
Please bear in mind that most UNIX machines store logging information in more than one log file, which is also the case with the Debian Linux machine used in this section. As a result, logFiles.go sends its output to multiple log files, which can be verified by the output of the following shell commands:
$ grep LOG_MAIL /var/log/mail.log
Oct 17 20:06:08 mail Some program![23688]: 2017/10/17 20:06:08 LOG_MAIL: Logging in Go!
$ grep LOG_LOCAL7 /var/log/cisco.log
Oct 17 20:06:08 mail logFiles[23688]: 2017/10/17 20:06:08 LOG_INFO + LOG_LOCAL7: Logging in Go!
$ grep LOG_ /var/log/syslog
Oct 17 20:06:08 mail logFiles[23688]: 2017/10/17 20:06:08 LOG_INFO + LOG_LOCAL7: Logging in Go!
Oct 17 20:06:08 mail Some program![23688]: 2017/10/17 20:06:08 LOG_MAIL: Logging in Go!
The preceding output shows that the message of the log.Println("LOG_INFO + LOG_LOCAL7: Logging in Go!") statement was written on both /var/log/cisco.log and /var/log/syslog, whereas the message of the log.Println("LOG_MAIL: Logging in Go!") statement was written on both /var/log/syslog and /var/log/mail.log.
The important thing to remember from this section is that if the logging server of a UNIX machine is not configured to catch all logging facilities, some of the log entries you send to it might get discarded without any warnings.