// Package userlogdir has a single function, UserLogDir, that returns // an OS-specific path for storing user logs. package userlogdir import ( "errors" "os" "runtime" ) // UserLogDir returns the default root directory to use for user-specific // log data. Users should create their own application-specific subdirectory // within this one and use that. // // On Unix systems, it returns $XDG_CACHE_HOME as specified by // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if // non-empty, else $HOME/.cache. // On Darwin, it returns $HOME/Library/Logs. // On Windows, it returns %LocalAppData%. // On Plan 9, it returns $home/lib/cache. // // If the location cannot be determined (for example, $HOME is not defined), // then it will return an error. func UserLogDir() (string, error) { var dir string switch runtime.GOOS { case "windows": dir = os.Getenv("LocalAppData") if dir == "" { return "", errors.New("%LocalAppData% is not defined") } case "darwin", "ios": dir = os.Getenv("HOME") if dir == "" { return "", errors.New("$HOME is not defined") } dir += "/Library/Logs" case "plan9": dir = os.Getenv("home") if dir == "" { return "", errors.New("$home is not defined") } dir += "/lib/cache" default: // Unix dir = os.Getenv("XDG_CACHE_HOME") if dir == "" { dir = os.Getenv("HOME") if dir == "" { return "", errors.New("neither $XDG_CACHE_HOME nor $HOME are defined") } dir += "/.cache" } } return dir, nil }