Merge pull request #1450 from influxdb/enable_file_logging

Enable file logging
pull/1455/head
Philip O'Toole 2015-01-29 15:18:10 -08:00
commit 3680b8769e
9 changed files with 60 additions and 20 deletions

View File

@ -115,8 +115,7 @@ type Config struct {
} `toml:"cluster"`
Logging struct {
File string `toml:"file"`
Level string `toml:"level"`
File string `toml:"file"`
} `toml:"logging"`
}

View File

@ -48,8 +48,6 @@ func TestParseConfig(t *testing.T) {
if c.Logging.File != "influxdb.log" {
t.Fatalf("logging file mismatch: %v", c.Logging.File)
} else if c.Logging.Level != "info" {
t.Fatalf("logging level mismatch: %v", c.Logging.Level)
}
if !c.Authentication.Enabled {
@ -169,8 +167,6 @@ join-urls = "http://127.0.0.1:8086"
enabled = true
[logging]
# logging level can be one of "debug", "info", "warn" or "error"
level = "info"
file = "influxdb.log"
# Configure the admin server

View File

@ -2,6 +2,7 @@ package main
import (
"flag"
"io"
"io/ioutil"
"log"
"net/http"
@ -33,7 +34,7 @@ func execRun(args []string) {
// Print sweet InfluxDB logo and write the process id to file.
log.Print(logo)
log.SetPrefix(`[srvr] `)
log.SetPrefix(`[influxd] `)
log.SetFlags(log.LstdFlags)
writePIDFile(*pidPath)
@ -42,6 +43,17 @@ func execRun(args []string) {
configExists := *configPath != ""
initializing := !fileExists(config.BrokerDir()) && !fileExists(config.DataDir())
// Create a logging writer.
logWriter := os.Stderr
if config.Logging.File != "" {
var err error
logWriter, err = os.OpenFile(config.Logging.File, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0660)
if err != nil {
log.Fatalf("unable to open log file %s: %s", config.Logging.File, err.Error())
}
}
log.SetOutput(logWriter)
// Parse join urls from the --join flag.
var joinURLs []*url.URL
if *join == "" {
@ -50,8 +62,11 @@ func execRun(args []string) {
joinURLs = parseURLs(*join)
}
// Mark the start of the log.
log.Printf("influxd starting up")
// Open broker, initialize or join as necessary.
b := openBroker(config.BrokerDir(), config.BrokerURL(), initializing, joinURLs)
b := openBroker(config.BrokerDir(), config.BrokerURL(), initializing, joinURLs, logWriter)
// Start the broker handler.
var h *Handler
@ -62,7 +77,7 @@ func execRun(args []string) {
}
// Open server, initialize or join as necessary.
s := openServer(config.DataDir(), config.DataURL(), b, initializing, configExists, joinURLs)
s := openServer(config.DataDir(), config.DataURL(), b, initializing, configExists, joinURLs, logWriter)
// Start the server handler. Attach to broker if listening on the same port.
if s != nil {
@ -161,7 +176,7 @@ func parseConfig(path, hostname string) *Config {
}
// creates and initializes a broker.
func openBroker(path string, u *url.URL, initializing bool, joinURLs []*url.URL) *messaging.Broker {
func openBroker(path string, u *url.URL, initializing bool, joinURLs []*url.URL, w io.Writer) *messaging.Broker {
// Ignore if there's no existing broker and we're not initializing or joining.
if !fileExists(path) && !initializing && len(joinURLs) == 0 {
return nil
@ -169,6 +184,7 @@ func openBroker(path string, u *url.URL, initializing bool, joinURLs []*url.URL)
// Create broker.
b := messaging.NewBroker()
b.SetLogOutput(w)
if err := b.Open(path, u); err != nil {
log.Fatalf("failed to open broker: %s", err)
}
@ -209,7 +225,7 @@ func joinBroker(b *messaging.Broker, joinURLs []*url.URL) {
}
// creates and initializes a server.
func openServer(path string, u *url.URL, b *messaging.Broker, initializing, configExists bool, joinURLs []*url.URL) *influxdb.Server {
func openServer(path string, u *url.URL, b *messaging.Broker, initializing, configExists bool, joinURLs []*url.URL, w io.Writer) *influxdb.Server {
// Ignore if there's no existing server and we're not initializing or joining.
if !fileExists(path) && !initializing && len(joinURLs) == 0 {
return nil
@ -217,6 +233,7 @@ func openServer(path string, u *url.URL, b *messaging.Broker, initializing, conf
// Create and open the server.
s := influxdb.NewServer()
s.SetLogOutput(w)
if err := s.Open(path); err != nil {
log.Fatalf("failed to open data server: %v", err.Error())
}
@ -224,30 +241,30 @@ func openServer(path string, u *url.URL, b *messaging.Broker, initializing, conf
// If the server is uninitialized then initialize or join it.
if initializing {
if len(joinURLs) == 0 {
initializeServer(s, b)
initializeServer(s, b, w)
} else {
joinServer(s, u, joinURLs)
openServerClient(s, joinURLs)
openServerClient(s, joinURLs, w)
}
} else if !configExists {
// We are spining up a server that has no config,
// but already has an initialized data directory
joinURLs = []*url.URL{b.URL()}
openServerClient(s, joinURLs)
openServerClient(s, joinURLs, w)
} else {
if len(joinURLs) == 0 {
// If a config exists, but no joinUrls are specified, fall back to the broker URL
// TODO: Make sure we have a leader, and then spin up the server
joinURLs = []*url.URL{b.URL()}
}
openServerClient(s, joinURLs)
openServerClient(s, joinURLs, w)
}
return s
}
// initializes a new server that does not yet have an ID.
func initializeServer(s *influxdb.Server, b *messaging.Broker) {
func initializeServer(s *influxdb.Server, b *messaging.Broker, w io.Writer) {
// TODO: Create replica using the messaging client.
// Create replica on broker.
@ -257,6 +274,7 @@ func initializeServer(s *influxdb.Server, b *messaging.Broker) {
// Create messaging client.
c := messaging.NewClient(1)
c.SetLogOutput(w)
if err := c.Open(filepath.Join(s.Path(), messagingClientFile), []*url.URL{b.URL()}); err != nil {
log.Fatalf("messaging client error: %s", err)
}
@ -287,8 +305,9 @@ func joinServer(s *influxdb.Server, u *url.URL, joinURLs []*url.URL) {
}
// opens the messaging client and attaches it to the server.
func openServerClient(s *influxdb.Server, joinURLs []*url.URL) {
func openServerClient(s *influxdb.Server, joinURLs []*url.URL, w io.Writer) {
c := messaging.NewClient(s.ID())
c.SetLogOutput(w)
if err := c.Open(filepath.Join(s.Path(), messagingClientFile), joinURLs); err != nil {
log.Fatalf("messaging client error: %s", err)
}

View File

@ -29,9 +29,7 @@ join-urls = ""
enabled = false
[logging]
# logging level can be one of "fine", "debug", "info", "warn" or "error"
level = "info"
file = "influxdb.log" # stdout to log to standard out, or syslog facility
file = "/var/log/influxdb/influxd.log"
# Configure the admin server
[admin]

View File

@ -59,6 +59,12 @@ func (b *Broker) metaPath() string {
func (b *Broker) opened() bool { return b.path != "" }
// SetLogOutput sets writer for all Broker log output.
func (b *Broker) SetLogOutput(w io.Writer) {
b.Logger = log.New(w, "[broker] ", log.LstdFlags)
b.log.SetLogOutput(w)
}
// Open initializes the log.
// The broker then must be initialized or join a cluster before it can be used.
func (b *Broker) Open(path string, u *url.URL) error {

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
@ -87,6 +88,11 @@ func (c *Client) LeaderURL() *url.URL {
return c.config.Brokers[0]
}
// SetLogOutput sets writer for all Client log output.
func (c *Client) SetLogOutput(w io.Writer) {
c.Logger = log.New(w, "[messaging] ", log.LstdFlags)
}
// Open initializes and opens the connection to the cluster. The
// URLs used to contact the cluster are either those supplied to
// the function, or if none are supplied, are read from the file

View File

@ -34,6 +34,7 @@ AWS_FILE=~/aws.conf
INSTALL_ROOT_DIR=/opt/influxdb
INFLUXDB_RUN_DIR=/var/opt/influxdb
INFLUXDB_LOG_DIR=/var/log/influxdb
CONFIG_ROOT_DIR=/etc/opt/influxdb
SAMPLE_CONFIGURATION=etc/config.sample.toml
@ -177,6 +178,8 @@ chmod -R a+rX $INSTALL_ROOT_DIR
mkdir -p $INFLUXDB_RUN_DIR
chown -R -L influxdb:influxdb $INFLUXDB_RUN_DIR
mkdir -p $INFLUXDB_LOG_DIR
chown -R -L influxdb:influxdb $INFLUXDB_LOG_DIR
EOF
echo "Post-install script created successfully at $POST_INSTALL_PATH"
}

View File

@ -206,6 +206,11 @@ func (l *Log) Config() *Config {
return nil
}
// SetLogOutput sets writer for all Raft output.
func (l *Log) SetLogOutput(w io.Writer) {
l.Logger = log.New(w, "[raft] ", log.LstdFlags)
}
// Open initializes the log from a path.
// If the path does not exist then it is created.
func (l *Log) Open(path string) error {

View File

@ -94,6 +94,8 @@ type Server struct {
shards map[uint64]*Shard // shards by shard id
shardsBySeriesID map[uint32][]*Shard // shards by series id
Logger *log.Logger
}
// NewServer returns a new instance of Server.
@ -107,6 +109,7 @@ func NewServer() *Server {
shards: make(map[uint64]*Shard),
shardsBySeriesID: make(map[uint32][]*Shard),
Logger: log.New(os.Stderr, "[server] ", log.LstdFlags),
}
}
@ -142,6 +145,11 @@ func (s *Server) metaPath() string {
return filepath.Join(s.path, "meta")
}
// SetLogOutput sets writer for all Server log output.
func (s *Server) SetLogOutput(w io.Writer) {
s.Logger = log.New(w, "[server] ", log.LstdFlags)
}
// Open initializes the server from a given path.
func (s *Server) Open(path string) error {
// Ensure the server isn't already open and there's a path provided.