Skip to content

Commit

Permalink
GCSFuse code change to write the logs to syslog files (#984)
Browse files Browse the repository at this point in the history
* Revert "Revert "GCSFuse code change to write the logs to syslog files" (#977)"

This reverts commit a393b36.

* Changing the behavior of logging

* Fixing linting issue

* Fixing linting issue

* Incorporating review comments
  • Loading branch information
raj-prince committed Mar 6, 2023
1 parent 4859983 commit b012d8f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 26 deletions.
90 changes: 65 additions & 25 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,64 @@ package logger
import (
"io"
"log"
"log/syslog"
"os"

"github.com/jacobsa/daemonize"
)

// Syslog file contains logs from all different programmes running on the VM.
// ProgrammeName is prefixed to all the logs written to syslog. This constant is
// used to filter the logs from syslog and write it to respective log files -
// gcsfuse.log in case of GCSFuse.
const ProgrammeName string = "gcsfuse"
const GCSFuseInBackgroundMode string = "GCSFUSE_IN_BACKGROUND_MODE"

var (
defaultLoggerFactory *loggerFactory
defaultInfoLogger *log.Logger
)

// InitLogFile initializes the logger factory to create loggers that print to
// a log file.
// In case of empty file, it starts writing the log to syslog file, which
// is eventually filtered and redirected to a fixed location using syslog
// config.
// Here, background true means, this InitLogFile has been called for the
// background daemon.
func InitLogFile(filename string, format string) error {
f, err := os.OpenFile(
filename,
os.O_WRONLY|os.O_CREATE|os.O_APPEND,
0644,
)
if err != nil {
return err
var f *os.File
var sysWriter *syslog.Writer
var err error
if filename != "" {
f, err = os.OpenFile(
filename,
os.O_WRONLY|os.O_CREATE|os.O_APPEND,
0644,
)
if err != nil {
return err
}
} else {
if _, ok := os.LookupEnv(GCSFuseInBackgroundMode); ok {
// Priority consist of facility and severity, here facility to specify the
// type of system that is logging the message to syslog and severity is log-level.
// User applications are allowed to take facility value between LOG_LOCAL0
// to LOG_LOCAL7. We are using LOG_LOCAL7 as facility and LOG_DEBUG to write
// debug messages.

// Suppressing the error while creating the syslog, although logger will
// be initialised with stdout/err, log will be printed anywhere. Because,
// in this case gcsfuse will be running as daemon.
sysWriter, _ = syslog.New(syslog.LOG_LOCAL7|syslog.LOG_DEBUG, ProgrammeName)
}
}

defaultLoggerFactory = &loggerFactory{
file: f,
flag: 0,
format: format,
file: f,
sysWriter: sysWriter,
flag: 0,
format: format,
}
defaultInfoLogger = NewInfo("")

Expand Down Expand Up @@ -103,29 +135,37 @@ func Info(v ...interface{}) {

type loggerFactory struct {
// If nil, log to stdout or stderr. Otherwise, log to this file.
file *os.File
flag int
format string
file *os.File
sysWriter *syslog.Writer
flag int
format string
}

func (f *loggerFactory) newLogger(level, prefix string) *log.Logger {
return log.New(f.writer(level), prefix, f.flag)
}

func (f *loggerFactory) createJsonOrTextWriter(level string, writer io.Writer) io.Writer {
if f.format == "json" {
return &jsonWriter{
w: writer,
level: level,
}
}

return &textWriter{
w: writer,
level: level,
}
}

func (f *loggerFactory) writer(level string) io.Writer {
if f.file != nil {
switch f.format {
case "json":
return &jsonWriter{
w: f.file,
level: level,
}
case "text":
return &textWriter{
w: f.file,
level: level,
}
}
return f.createJsonOrTextWriter(level, f.file)
}

if f.sysWriter != nil {
return f.createJsonOrTextWriter(level, f.sysWriter)
}

switch level {
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func runCLIApp(c *cli.Context) (err error) {
return fmt.Errorf("parsing flags failed: %w", err)
}

if flags.Foreground && flags.LogFile != "" {
if flags.Foreground {
err = logger.InitLogFile(flags.LogFile, flags.LogFormat)
if err != nil {
return fmt.Errorf("init log file: %w", err)
Expand Down Expand Up @@ -347,6 +347,11 @@ func runCLIApp(c *cli.Context) (err error) {
env = append(env, fmt.Sprintf("HOME=%s", homeDir))
}

// This environment variable will be helpful to distinguish b/w the main
// process and daemon process. If this environment variable set that means
// programme is running as daemon process.
env = append(env, fmt.Sprintf("%s=true", logger.GCSFuseInBackgroundMode))

// Run.
err = daemonize.Run(path, args, env, os.Stdout)
if err != nil {
Expand Down

0 comments on commit b012d8f

Please sign in to comment.