github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/main_remote_supported.go (about)

     1  // +build remoteclient
     2  // +build linux darwin
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	"github.com/containers/libpod/pkg/util"
    12  	"github.com/pkg/errors"
    13  	"github.com/sirupsen/logrus"
    14  )
    15  
    16  func setSyslog() error {
    17  	var err error
    18  	cfgHomeDir := os.Getenv("XDG_CONFIG_HOME")
    19  	if cfgHomeDir == "" {
    20  		if cfgHomeDir, err = util.GetRootlessConfigHomeDir(); err != nil {
    21  			return err
    22  		}
    23  		if err = os.Setenv("XDG_CONFIG_HOME", cfgHomeDir); err != nil {
    24  			return errors.Wrapf(err, "cannot set XDG_CONFIG_HOME")
    25  		}
    26  	}
    27  	path := filepath.Join(cfgHomeDir, "containers")
    28  
    29  	// Log to file if not using syslog
    30  
    31  	if _, err := os.Stat(path); os.IsNotExist(err) {
    32  		if err := os.MkdirAll(path, 0750); err != nil {
    33  			fmt.Fprintf(os.Stderr, "%v", err)
    34  			return err
    35  		}
    36  	}
    37  
    38  	// Update path to include file name
    39  	path = filepath.Join(path, "podman.log")
    40  
    41  	// Create the log file if doesn't exist. And append to it if it already exists.
    42  	file, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0640)
    43  	if err != nil {
    44  		// Cannot open log file. Logging to stderr
    45  		fmt.Fprintf(os.Stderr, "%v", err)
    46  		return err
    47  	} else {
    48  		formatter := new(logrus.TextFormatter)
    49  		formatter.FullTimestamp = true
    50  		logrus.SetFormatter(formatter)
    51  		logrus.SetOutput(file)
    52  	}
    53  
    54  	// Note this message is only logged if --log-level >= Info!
    55  	logrus.Infof("Logging level set to %s", logrus.GetLevel().String())
    56  	return nil
    57  }