×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang program to add the full path of program file in log message as a prefix using log.SetFlags() function

Here, we are going to learn how to add the full path of program file in log message as a prefix using log.SetFlags() function in Golang (Go Language)?
Submitted by Nidhi, on April 23, 2021 [Last updated : March 05, 2023]

How to add the full path of program file in log message as a prefix in Golang?

Problem Solution:

Here, we will use log.SetFlags() function to add full of program file in log message as a prefix. Here, we will pass log.Llongfile constant in log.SetFlags() function.

Program/Source Code:

The source code to add the full path of the program file in log message as a prefix using log.SetFlags() function is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

Golang code to add the full path of program file in log message as a prefix using log.SetFlags() function

// Golang program to set full path of
// program file in the log message.

package main

import "log"
import "fmt"

func main() {
	log.SetFlags(log.Llongfile)

	log.Println("Log Line1")
	log.Println("Log Line2")

	fmt.Println("Program finished")
}

Output:

/home/main.go:12: Log Line1
/home/main.go:13: Log Line2
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 added the complete path of the program file as a prefix in every line of the log message using log.Llongfile constant in logSetFlags() function.

Golang log Package Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.