mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
logging: Support placeholders in level and filename (#2872)
* Add support for placeholders in Config Fixes #2870 * Replace placeholders only in logging config. Placeholders in log level and filename incase of file output are replaced. * Add Provision to filewriter module for replacing placeholders
This commit is contained in:
parent
7ff02f37b6
commit
dfdddcfacb
2 changed files with 29 additions and 2 deletions
11
logging.go
11
logging.go
|
@ -280,8 +280,15 @@ type CustomLog struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cl *CustomLog) provision(ctx Context, logging *Logging) error {
|
func (cl *CustomLog) provision(ctx Context, logging *Logging) error {
|
||||||
|
// Replace placeholder for log level
|
||||||
|
repl := NewReplacer()
|
||||||
|
level, err := repl.ReplaceOrErr(cl.Level, true, true)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid log level: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// set up the log level
|
// set up the log level
|
||||||
switch cl.Level {
|
switch level {
|
||||||
case "debug":
|
case "debug":
|
||||||
cl.levelEnabler = zapcore.DebugLevel
|
cl.levelEnabler = zapcore.DebugLevel
|
||||||
case "", "info":
|
case "", "info":
|
||||||
|
@ -351,7 +358,7 @@ func (cl *CustomLog) provision(ctx Context, logging *Logging) error {
|
||||||
if cl.writerOpener == nil {
|
if cl.writerOpener == nil {
|
||||||
cl.writerOpener = StderrWriter{}
|
cl.writerOpener = StderrWriter{}
|
||||||
}
|
}
|
||||||
var err error
|
|
||||||
cl.writer, _, err = logging.openWriter(cl.writerOpener)
|
cl.writer, _, err = logging.openWriter(cl.writerOpener)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("opening log writer using %#v: %v", cl.writerOpener, err)
|
return fmt.Errorf("opening log writer using %#v: %v", cl.writerOpener, err)
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
package logging
|
package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -46,6 +47,19 @@ func (FileWriter) CaddyModule() caddy.ModuleInfo {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Provision sets up the module
|
||||||
|
func (fw *FileWriter) Provision(ctx caddy.Context) error {
|
||||||
|
// Replace placeholder in filename
|
||||||
|
repl := caddy.NewReplacer()
|
||||||
|
filename, err := repl.ReplaceOrErr(fw.Filename, true, true)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid filename for log file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fw.Filename = filename
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (fw FileWriter) String() string {
|
func (fw FileWriter) String() string {
|
||||||
fpath, err := filepath.Abs(fw.Filename)
|
fpath, err := filepath.Abs(fw.Filename)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -76,6 +90,7 @@ func (fw FileWriter) OpenWriter() (io.WriteCloser, error) {
|
||||||
if fw.RollKeepDays == 0 {
|
if fw.RollKeepDays == 0 {
|
||||||
fw.RollKeepDays = 90
|
fw.RollKeepDays = 90
|
||||||
}
|
}
|
||||||
|
|
||||||
return &lumberjack.Logger{
|
return &lumberjack.Logger{
|
||||||
Filename: fw.Filename,
|
Filename: fw.Filename,
|
||||||
MaxSize: fw.RollSizeMB,
|
MaxSize: fw.RollSizeMB,
|
||||||
|
@ -89,3 +104,8 @@ func (fw FileWriter) OpenWriter() (io.WriteCloser, error) {
|
||||||
// otherwise just open a regular file
|
// otherwise just open a regular file
|
||||||
return os.OpenFile(fw.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
return os.OpenFile(fw.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Interface guards
|
||||||
|
var (
|
||||||
|
_ caddy.Provisioner = (*FileWriter)(nil)
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue