2015-04-09 00:23:30 -04:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-05-29 17:00:46 -04:00
|
|
|
"os"
|
|
|
|
"strings"
|
2015-04-09 00:23:30 -04:00
|
|
|
"sync"
|
2015-05-29 17:00:46 -04:00
|
|
|
"time"
|
2015-04-09 00:23:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Creator is a method that builds a logging driver instance with given context
|
|
|
|
type Creator func(Context) (Logger, error)
|
|
|
|
|
2015-06-30 20:40:13 -04:00
|
|
|
//LogOptValidator is a method that validates the log opts provided
|
|
|
|
type LogOptValidator func(cfg map[string]string) error
|
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
// Context provides enough information for a logging driver to do its function
|
|
|
|
type Context struct {
|
2015-05-29 17:00:46 -04:00
|
|
|
Config map[string]string
|
|
|
|
ContainerID string
|
|
|
|
ContainerName string
|
|
|
|
ContainerEntrypoint string
|
|
|
|
ContainerArgs []string
|
|
|
|
ContainerImageID string
|
|
|
|
ContainerImageName string
|
|
|
|
ContainerCreated time.Time
|
|
|
|
LogPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) Hostname() (string, error) {
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("logger: can not resolve hostname: %v", err)
|
|
|
|
}
|
|
|
|
return hostname, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) Command() string {
|
|
|
|
terms := []string{ctx.ContainerEntrypoint}
|
|
|
|
for _, arg := range ctx.ContainerArgs {
|
|
|
|
terms = append(terms, arg)
|
|
|
|
}
|
|
|
|
command := strings.Join(terms, " ")
|
|
|
|
return command
|
2015-04-09 00:23:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type logdriverFactory struct {
|
2015-06-30 20:40:13 -04:00
|
|
|
registry map[string]Creator
|
|
|
|
optValidator map[string]LogOptValidator
|
|
|
|
m sync.Mutex
|
2015-04-09 00:23:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lf *logdriverFactory) register(name string, c Creator) error {
|
|
|
|
lf.m.Lock()
|
|
|
|
defer lf.m.Unlock()
|
|
|
|
|
|
|
|
if _, ok := lf.registry[name]; ok {
|
|
|
|
return fmt.Errorf("logger: log driver named '%s' is already registered", name)
|
|
|
|
}
|
|
|
|
lf.registry[name] = c
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:40:13 -04:00
|
|
|
func (lf *logdriverFactory) registerLogOptValidator(name string, l LogOptValidator) error {
|
|
|
|
lf.m.Lock()
|
|
|
|
defer lf.m.Unlock()
|
|
|
|
|
|
|
|
if _, ok := lf.optValidator[name]; ok {
|
|
|
|
return fmt.Errorf("logger: log driver named '%s' is already registered", name)
|
|
|
|
}
|
|
|
|
lf.optValidator[name] = l
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
func (lf *logdriverFactory) get(name string) (Creator, error) {
|
|
|
|
lf.m.Lock()
|
|
|
|
defer lf.m.Unlock()
|
|
|
|
|
|
|
|
c, ok := lf.registry[name]
|
|
|
|
if !ok {
|
|
|
|
return c, fmt.Errorf("logger: no log driver named '%s' is registered", name)
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:40:13 -04:00
|
|
|
func (lf *logdriverFactory) getLogOptValidator(name string) LogOptValidator {
|
|
|
|
lf.m.Lock()
|
|
|
|
defer lf.m.Unlock()
|
|
|
|
|
|
|
|
c, _ := lf.optValidator[name]
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
var factory = &logdriverFactory{registry: make(map[string]Creator), optValidator: make(map[string]LogOptValidator)} // global factory instance
|
2015-04-09 00:23:30 -04:00
|
|
|
|
|
|
|
// RegisterLogDriver registers the given logging driver builder with given logging
|
|
|
|
// driver name.
|
|
|
|
func RegisterLogDriver(name string, c Creator) error {
|
|
|
|
return factory.register(name, c)
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:40:13 -04:00
|
|
|
func RegisterLogOptValidator(name string, l LogOptValidator) error {
|
|
|
|
return factory.registerLogOptValidator(name, l)
|
|
|
|
}
|
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
// GetLogDriver provides the logging driver builder for a logging driver name.
|
|
|
|
func GetLogDriver(name string) (Creator, error) {
|
|
|
|
return factory.get(name)
|
|
|
|
}
|
2015-06-30 20:40:13 -04:00
|
|
|
|
|
|
|
func ValidateLogOpts(name string, cfg map[string]string) error {
|
|
|
|
l := factory.getLogOptValidator(name)
|
|
|
|
if l != nil {
|
|
|
|
return l(cfg)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Log Opts are not valid for [%s] driver", name)
|
|
|
|
}
|