/
juju-log.go
78 lines (66 loc) · 1.79 KB
/
juju-log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuc
import (
"errors"
"fmt"
"strings"
"github.com/juju/cmd"
"github.com/juju/loggo"
"launchpad.net/gnuflag"
)
// JujuLogCommand implements the juju-log command.
type JujuLogCommand struct {
cmd.CommandBase
ctx Context
Message string
Debug bool
Level string
formatFlag string // deprecated
}
func NewJujuLogCommand(ctx Context) cmd.Command {
return &JujuLogCommand{ctx: ctx}
}
func (c *JujuLogCommand) Info() *cmd.Info {
return &cmd.Info{
Name: "juju-log",
Args: "<message>",
Purpose: "write a message to the juju log",
}
}
func (c *JujuLogCommand) SetFlags(f *gnuflag.FlagSet) {
f.BoolVar(&c.Debug, "debug", false, "log at debug level")
f.StringVar(&c.Level, "l", "INFO", "Send log message at the given level")
f.StringVar(&c.Level, "log-level", "INFO", "")
f.StringVar(&c.formatFlag, "format", "", "deprecated format flag")
}
func (c *JujuLogCommand) Init(args []string) error {
if args == nil {
return errors.New("no message specified")
}
c.Message = strings.Join(args, " ")
return nil
}
func (c *JujuLogCommand) Run(ctx *cmd.Context) error {
if c.formatFlag != "" {
fmt.Fprintf(ctx.Stderr, "--format flag deprecated for command %q", c.Info().Name)
}
logger := loggo.GetLogger(fmt.Sprintf("unit.%s.juju-log", c.ctx.UnitName()))
logLevel := loggo.INFO
if c.Debug {
logLevel = loggo.DEBUG
} else if c.Level != "" {
var ok bool
logLevel, ok = loggo.ParseLevel(c.Level)
if !ok {
logger.Warningf("Specified log level of %q is not valid", c.Level)
logLevel = loggo.INFO
}
}
prefix := ""
if r, found := c.ctx.HookRelation(); found {
prefix = r.FakeId() + ": "
}
logger.Logf(logLevel, "%s%s", prefix, c.Message)
return nil
}