Allow access and error logging to stdout by configuring a path of "-".

Thanks to @icedquinn@blob.cat for the suggestion.
This commit is contained in:
Solderpunk 2021-01-24 17:09:47 +01:00
parent e06f8bddbc
commit 92cd40db12
2 changed files with 23 additions and 11 deletions

View File

@ -198,10 +198,12 @@ examples of the appropriate syntax.
`/var/gemini/users/gus/` to `/home/gus/public_gemini/` if you want. `/var/gemini/users/gus/` to `/home/gus/public_gemini/` if you want.
* `AccessLog`: Path to access log file (default value `access.log`, * `AccessLog`: Path to access log file (default value `access.log`,
i.e. in the current wrorking directory). Note that all intermediate i.e. in the current wrorking directory). Note that all intermediate
directories must exist, Molly Brown won't create them for you. directories must exist, Molly Brown won't create them for you. Set
to `-` for logging to `stdout`.
* `ErrorLog`: Path to error log file (default value `error.log`, i.e. * `ErrorLog`: Path to error log file (default value `error.log`, i.e.
in the current wrorking directory). Note that all intermediate in the current wrorking directory). Note that all intermediate
directories must exist, Molly Brown won't create them for you. directories must exist, Molly Brown won't create them for you. Set
to `-` for logging to `stdout`.
* `GeminiExt`: Files with this extension will be served with a MIME * `GeminiExt`: Files with this extension will be served with a MIME
type of `text/gemini` (default value `gmi`). type of `text/gemini` (default value `gmi`).
* `MimeOverrides`: In this section of the config file, keys are path * `MimeOverrides`: In this section of the config file, keys are path

28
main.go
View File

@ -26,19 +26,29 @@ func main() {
} }
// Open log files // Open log files
errorLogFile, err := os.OpenFile(config.ErrorLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) var errorLogFile *os.File
if err != nil { if config.ErrorLog == "-" {
log.Fatal(err) errorLogFile = os.Stdout
} else {
errorLogFile, err = os.OpenFile(config.ErrorLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer errorLogFile.Close()
} }
defer errorLogFile.Close()
errorLog := log.New(errorLogFile, "", log.Ldate | log.Ltime) errorLog := log.New(errorLogFile, "", log.Ldate | log.Ltime)
accessLogFile, err := os.OpenFile(config.AccessLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) var accessLogFile *os.File
if err != nil { if config.AccessLog == "-" {
errorLog.Println("Error opening access log file: " + err.Error()) accessLogFile = os.Stdout
log.Fatal(err) } else {
accessLogFile, err = os.OpenFile(config.AccessLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
errorLog.Println("Error opening access log file: " + err.Error())
log.Fatal(err)
}
defer accessLogFile.Close()
} }
defer accessLogFile.Close()
// Read TLS files, create TLS config // Read TLS files, create TLS config
cert, err := tls.LoadX509KeyPair(config.CertPath, config.KeyPath) cert, err := tls.LoadX509KeyPair(config.CertPath, config.KeyPath)