mirror of
https://github.com/coredns/coredns.git
synced 2025-11-14 16:02:16 -05:00
middleware/{log,errors}: output everything to stdout (#684)
Limit the options in both errors and log middleware, just output to stdout and let someone else (journald,docker) care about where to route the logs. This removes syslog and logging to a file. Fixes #573 #602
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# log
|
||||
|
||||
*log* enables query logging.
|
||||
*log* enables query logging to standard output.
|
||||
|
||||
## Syntax
|
||||
|
||||
@@ -8,20 +8,20 @@
|
||||
log
|
||||
~~~
|
||||
|
||||
* With no arguments, a query log entry is written to query.log in the common log format for all requests
|
||||
* With no arguments, a query log entry is written to *stdout* in the common log format for all requests
|
||||
|
||||
~~~ txt
|
||||
log FILE
|
||||
~~~
|
||||
|
||||
* **FILE** is the log file to create (or append to).
|
||||
* **FILE** is the log file to create (or append to). The *only* valid name for **FILE** is *stdout*.
|
||||
|
||||
~~~ txt
|
||||
log [NAME] FILE [FORMAT]
|
||||
~~~
|
||||
|
||||
* `NAME` is the name to match in order to be logged
|
||||
* `FILE` is the log file to create (or append to)
|
||||
* `FILE` is the log file (again only *stdout* is allowed here).
|
||||
* `FORMAT` is the log format to use (default is Common Log Format)
|
||||
|
||||
You can further specify the class of responses that get logged:
|
||||
@@ -45,9 +45,8 @@ If no class is specified, it defaults to *all*.
|
||||
|
||||
## Log File
|
||||
|
||||
The log file can be any filename. It could also be *stdout* or *stderr* to write the log to the console,
|
||||
or *syslog* to write to the system log (except on Windows). If the log file does not exist beforehand,
|
||||
CoreDNS will create it before appending to it.
|
||||
The "log file" can only be *stdout*. CoreDNS expects another service to pick up this output and deal
|
||||
with it, i.e. journald when using systemd or Docker's logging capabilities.
|
||||
|
||||
## Log Format
|
||||
|
||||
@@ -80,22 +79,22 @@ The default Common Log Format is:
|
||||
|
||||
## Examples
|
||||
|
||||
Log all requests to a file:
|
||||
Log all requests to stdout
|
||||
|
||||
~~~
|
||||
log /var/log/query.log
|
||||
log stdout
|
||||
~~~
|
||||
|
||||
Custom log format:
|
||||
Custom log format, for all zones (`.`)
|
||||
|
||||
~~~
|
||||
log . ../query.log "{proto} Request: {name} {type} {>id}"
|
||||
log . stdout "{proto} Request: {name} {type} {>id}"
|
||||
~~~
|
||||
|
||||
Only log denials for example.org (and below to a file)
|
||||
|
||||
~~~
|
||||
log example.org example-query-log {
|
||||
log example.org stdout {
|
||||
class denial
|
||||
}
|
||||
~~~
|
||||
|
||||
@@ -78,8 +78,8 @@ type Rule struct {
|
||||
}
|
||||
|
||||
const (
|
||||
// DefaultLogFilename is the default log filename.
|
||||
DefaultLogFilename = "query.log"
|
||||
// DefaultLogFilename is the default output name.
|
||||
DefaultLogFilename = "stdout"
|
||||
// CommonLogFormat is the common log format.
|
||||
CommonLogFormat = `{remote} ` + CommonLogEmptyValue + ` [{when}] "{type} {class} {name} {proto} {size} {>do} {>bufsize}" {rcode} {rsize} {duration}`
|
||||
// CommonLogEmptyValue is the common empty log value.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"io"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/coredns/coredns/middleware"
|
||||
"github.com/coredns/coredns/middleware/pkg/response"
|
||||
|
||||
"github.com/hashicorp/go-syslog"
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
@@ -30,25 +29,10 @@ func setup(c *caddy.Controller) error {
|
||||
// Open the log files for writing when the server starts
|
||||
c.OnStartup(func() error {
|
||||
for i := 0; i < len(rules); i++ {
|
||||
var err error
|
||||
var writer io.Writer
|
||||
|
||||
if rules[i].OutputFile == "stdout" {
|
||||
writer = os.Stdout
|
||||
} else if rules[i].OutputFile == "stderr" {
|
||||
writer = os.Stderr
|
||||
} else if rules[i].OutputFile == "syslog" {
|
||||
writer, err = gsyslog.NewLogger(gsyslog.LOG_INFO, "LOCAL0", "coredns")
|
||||
if err != nil {
|
||||
return middleware.Error("log", err)
|
||||
}
|
||||
} else {
|
||||
var file *os.File
|
||||
file, err = os.OpenFile(rules[i].OutputFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
return middleware.Error("log", err)
|
||||
}
|
||||
writer = file
|
||||
// We only support stdout
|
||||
writer := os.Stdout
|
||||
if rules[i].OutputFile != "stdout" {
|
||||
return middleware.Error("log", fmt.Errorf("invalid log file: %s", rules[i].OutputFile))
|
||||
}
|
||||
|
||||
rules[i].Log = log.New(writer, "", 0)
|
||||
@@ -78,7 +62,7 @@ func logParse(c *caddy.Controller) ([]Rule, error) {
|
||||
Format: DefaultLogFormat,
|
||||
})
|
||||
} else if len(args) == 1 {
|
||||
// Only an output file specified
|
||||
// Only an output file specified.
|
||||
rules = append(rules, Rule{
|
||||
NameScope: ".",
|
||||
OutputFile: args[0],
|
||||
|
||||
@@ -45,7 +45,7 @@ func TestLogParse(t *testing.T) {
|
||||
Format: CombinedLogFormat,
|
||||
}}},
|
||||
{`log example.org. log.txt
|
||||
log example.net accesslog.txt {combined}`, false, []Rule{{
|
||||
log example.net accesslog.txt {combined}`, false, []Rule{{
|
||||
NameScope: "example.org.",
|
||||
OutputFile: "log.txt",
|
||||
Format: DefaultLogFormat,
|
||||
|
||||
Reference in New Issue
Block a user