Q:

Golang program to set date and time flags using log.SetFlags() for log messages

belongs to collection: Golang log Package Programs

0

Here, we will use a log.SetFlags() function with some constants of log package to set flags for log messages.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to set date and time flags using log.SetFlags() for log messages is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Golang program to set date and time flags
// using log.SetFlags() for log messages

package main

import "log"
import "fmt"

func main() {
	log.SetFlags(log.Ldate)
	log.Println("Log line1")

	log.SetFlags(log.Ltime)
	log.Println("Log line2")

	log.SetFlags(log.Lmicroseconds)
	log.Println("Log line3")

	log.SetFlags(log.Ltime | log.LUTC)
	log.Println("Log line4")

	fmt.Println("Program finished")
}

Output:

2021/04/23 Log line1
16:30:22 Log line2
16:30:22.342067 Log line3
16:30:22 Log line4
Program finished

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the "fmt" package to use the Println() function and we also imported the "log" package to use the log functions.

In the main() function, we set date and time flags using log.SetFlags() function. The log.SetFlags() function is used to set some prefix value to the log messages.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Golang program to add the full path of program fil... >>
<< Golang program to demonstrate the log.SetPrefix() ...