diff --git a/dnscrypt-proxy/example-dnscrypt-proxy.toml b/dnscrypt-proxy/example-dnscrypt-proxy.toml index 15c7e385..7a36cacc 100644 --- a/dnscrypt-proxy/example-dnscrypt-proxy.toml +++ b/dnscrypt-proxy/example-dnscrypt-proxy.toml @@ -406,7 +406,7 @@ cache_neg_max_ttl = 600 [query_log] ## Path to the query log file (absolute, or relative to the same directory as the config file) - ## On non-Windows systems, can be /dev/stdout to log to the standard output (also set log_files_max_size to 0) + ## Can be set to /dev/stdout in order to log to the standard output. # file = 'query.log' diff --git a/dnscrypt-proxy/logger.go b/dnscrypt-proxy/logger.go new file mode 100644 index 00000000..03e40d66 --- /dev/null +++ b/dnscrypt-proxy/logger.go @@ -0,0 +1,28 @@ +package main + +import ( + "io" + "os" + + "github.com/jedisct1/dlog" + "gopkg.in/natefinch/lumberjack.v2" +) + +func Logger(logMaxSize int, logMaxAge int, logMaxBackups int, fileName string) io.Writer { + if fileName == "/dev/stdout" { + return os.Stdout + } + if st, _ := os.Stat(fileName); st != nil && !st.Mode().IsRegular() { + if st.Mode().IsDir() { + dlog.Fatalf("[%v] is a directory", fileName) + } + fp, err := os.OpenFile(fileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + if err != nil { + dlog.Fatalf("Unable to access [%v]: [%v]", fileName, err) + } + return fp + } + logger := &lumberjack.Logger{LocalTime: true, MaxSize: logMaxSize, MaxAge: logMaxAge, MaxBackups: logMaxBackups, Filename: fileName, Compress: true} + + return logger +} diff --git a/dnscrypt-proxy/plugin_block_ip.go b/dnscrypt-proxy/plugin_block_ip.go index b0e9239f..52b7f8f9 100644 --- a/dnscrypt-proxy/plugin_block_ip.go +++ b/dnscrypt-proxy/plugin_block_ip.go @@ -11,7 +11,6 @@ import ( iradix "github.com/hashicorp/go-immutable-radix" "github.com/jedisct1/dlog" "github.com/miekg/dns" - "gopkg.in/natefinch/lumberjack.v2" ) type PluginBlockIP struct { @@ -72,7 +71,7 @@ func (plugin *PluginBlockIP) Init(proxy *Proxy) error { if len(proxy.blockIPLogFile) == 0 { return nil } - plugin.logger = &lumberjack.Logger{LocalTime: true, MaxSize: proxy.logMaxSize, MaxAge: proxy.logMaxAge, MaxBackups: proxy.logMaxBackups, Filename: proxy.blockIPLogFile, Compress: true} + plugin.logger = Logger(proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups, proxy.blockIPLogFile) plugin.format = proxy.blockIPFormat return nil diff --git a/dnscrypt-proxy/plugin_block_name.go b/dnscrypt-proxy/plugin_block_name.go index fbfdc5d6..74dadc51 100644 --- a/dnscrypt-proxy/plugin_block_name.go +++ b/dnscrypt-proxy/plugin_block_name.go @@ -11,7 +11,6 @@ import ( "github.com/jedisct1/dlog" "github.com/miekg/dns" - lumberjack "gopkg.in/natefinch/lumberjack.v2" ) type BlockedNames struct { @@ -126,7 +125,7 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error { if len(proxy.blockNameLogFile) == 0 { return nil } - blockedNames.logger = &lumberjack.Logger{LocalTime: true, MaxSize: proxy.logMaxSize, MaxAge: proxy.logMaxAge, MaxBackups: proxy.logMaxBackups, Filename: proxy.blockNameLogFile, Compress: true} + blockedNames.logger = Logger(proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups, proxy.blockNameLogFile) blockedNames.format = proxy.blockNameFormat return nil diff --git a/dnscrypt-proxy/plugin_nx_log.go b/dnscrypt-proxy/plugin_nx_log.go index 440a7453..fce62a68 100644 --- a/dnscrypt-proxy/plugin_nx_log.go +++ b/dnscrypt-proxy/plugin_nx_log.go @@ -9,7 +9,6 @@ import ( "github.com/jedisct1/dlog" "github.com/miekg/dns" - lumberjack "gopkg.in/natefinch/lumberjack.v2" ) type PluginNxLog struct { @@ -26,7 +25,7 @@ func (plugin *PluginNxLog) Description() string { } func (plugin *PluginNxLog) Init(proxy *Proxy) error { - plugin.logger = &lumberjack.Logger{LocalTime: true, MaxSize: proxy.logMaxSize, MaxAge: proxy.logMaxAge, MaxBackups: proxy.logMaxBackups, Filename: proxy.nxLogFile, Compress: true} + plugin.logger = Logger(proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups, proxy.nxLogFile) plugin.format = proxy.nxLogFormat return nil diff --git a/dnscrypt-proxy/plugin_query_log.go b/dnscrypt-proxy/plugin_query_log.go index d32812f3..5a806647 100644 --- a/dnscrypt-proxy/plugin_query_log.go +++ b/dnscrypt-proxy/plugin_query_log.go @@ -10,7 +10,6 @@ import ( "github.com/jedisct1/dlog" "github.com/miekg/dns" - lumberjack "gopkg.in/natefinch/lumberjack.v2" ) type PluginQueryLog struct { @@ -28,7 +27,7 @@ func (plugin *PluginQueryLog) Description() string { } func (plugin *PluginQueryLog) Init(proxy *Proxy) error { - plugin.logger = &lumberjack.Logger{LocalTime: true, MaxSize: proxy.logMaxSize, MaxAge: proxy.logMaxAge, MaxBackups: proxy.logMaxBackups, Filename: proxy.queryLogFile, Compress: true} + plugin.logger = Logger(proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups, proxy.queryLogFile) plugin.format = proxy.queryLogFormat plugin.ignoredQtypes = proxy.queryLogIgnoredQtypes diff --git a/dnscrypt-proxy/plugin_whitelist_name.go b/dnscrypt-proxy/plugin_whitelist_name.go index c33f3f20..c086b2f1 100644 --- a/dnscrypt-proxy/plugin_whitelist_name.go +++ b/dnscrypt-proxy/plugin_whitelist_name.go @@ -11,7 +11,6 @@ import ( "github.com/jedisct1/dlog" "github.com/miekg/dns" - lumberjack "gopkg.in/natefinch/lumberjack.v2" ) type PluginWhitelistName struct { @@ -68,7 +67,7 @@ func (plugin *PluginWhitelistName) Init(proxy *Proxy) error { if len(proxy.whitelistNameLogFile) == 0 { return nil } - plugin.logger = &lumberjack.Logger{LocalTime: true, MaxSize: proxy.logMaxSize, MaxAge: proxy.logMaxAge, MaxBackups: proxy.logMaxBackups, Filename: proxy.whitelistNameLogFile, Compress: true} + plugin.logger = Logger(proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups, proxy.whitelistNameLogFile) plugin.format = proxy.whitelistNameFormat return nil