mirror of
https://github.com/containers/podman.git
synced 2025-09-27 16:54:42 +08:00
Merge pull request #5244 from Akasurde/i4962
Add cmd flag to show container name in log
This commit is contained in:
@ -260,6 +260,7 @@ type LogsValues struct {
|
|||||||
Tail int64
|
Tail int64
|
||||||
Timestamps bool
|
Timestamps bool
|
||||||
Latest bool
|
Latest bool
|
||||||
|
UseName bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type MountValues struct {
|
type MountValues struct {
|
||||||
|
@ -37,6 +37,7 @@ var (
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Example: `podman logs ctrID
|
Example: `podman logs ctrID
|
||||||
|
podman logs --names ctrID1 ctrID2
|
||||||
podman logs --tail 2 mywebserver
|
podman logs --tail 2 mywebserver
|
||||||
podman logs --follow=true --since 10m ctrID
|
podman logs --follow=true --since 10m ctrID
|
||||||
podman logs mywebserver mydbserver`,
|
podman logs mywebserver mydbserver`,
|
||||||
@ -54,6 +55,7 @@ func init() {
|
|||||||
flags.StringVar(&logsCommand.Since, "since", "", "Show logs since TIMESTAMP")
|
flags.StringVar(&logsCommand.Since, "since", "", "Show logs since TIMESTAMP")
|
||||||
flags.Int64Var(&logsCommand.Tail, "tail", -1, "Output the specified number of LINES at the end of the logs. Defaults to -1, which prints all lines")
|
flags.Int64Var(&logsCommand.Tail, "tail", -1, "Output the specified number of LINES at the end of the logs. Defaults to -1, which prints all lines")
|
||||||
flags.BoolVarP(&logsCommand.Timestamps, "timestamps", "t", false, "Output the timestamps in the log")
|
flags.BoolVarP(&logsCommand.Timestamps, "timestamps", "t", false, "Output the timestamps in the log")
|
||||||
|
flags.BoolVarP(&logsCommand.UseName, "names", "n", false, "Output the container name in the log")
|
||||||
markFlagHidden(flags, "details")
|
markFlagHidden(flags, "details")
|
||||||
flags.SetInterspersed(false)
|
flags.SetInterspersed(false)
|
||||||
|
|
||||||
@ -85,6 +87,7 @@ func logsCmd(c *cliconfig.LogsValues) error {
|
|||||||
Since: sinceTime,
|
Since: sinceTime,
|
||||||
Tail: c.Tail,
|
Tail: c.Tail,
|
||||||
Timestamps: c.Timestamps,
|
Timestamps: c.Timestamps,
|
||||||
|
UseName: c.UseName,
|
||||||
}
|
}
|
||||||
return runtime.Log(c, options)
|
return runtime.Log(c, options)
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,7 @@ func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *l
|
|||||||
if len(tailLog) > 0 {
|
if len(tailLog) > 0 {
|
||||||
for _, nll := range tailLog {
|
for _, nll := range tailLog {
|
||||||
nll.CID = c.ID()
|
nll.CID = c.ID()
|
||||||
|
nll.CName = c.Name()
|
||||||
if nll.Since(options.Since) {
|
if nll.Since(options.Since) {
|
||||||
logChannel <- nll
|
logChannel <- nll
|
||||||
}
|
}
|
||||||
@ -63,6 +64,7 @@ func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *l
|
|||||||
partial = ""
|
partial = ""
|
||||||
}
|
}
|
||||||
nll.CID = c.ID()
|
nll.CID = c.ID()
|
||||||
|
nll.CName = c.Name()
|
||||||
if nll.Since(options.Since) {
|
if nll.Since(options.Since) {
|
||||||
logChannel <- nll
|
logChannel <- nll
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ type LogOptions struct {
|
|||||||
Timestamps bool
|
Timestamps bool
|
||||||
Multi bool
|
Multi bool
|
||||||
WaitGroup *sync.WaitGroup
|
WaitGroup *sync.WaitGroup
|
||||||
|
UseName bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// LogLine describes the information for each line of a log
|
// LogLine describes the information for each line of a log
|
||||||
@ -47,6 +48,7 @@ type LogLine struct {
|
|||||||
Time time.Time
|
Time time.Time
|
||||||
Msg string
|
Msg string
|
||||||
CID string
|
CID string
|
||||||
|
CName string
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLogFile returns an hp tail for a container given options
|
// GetLogFile returns an hp tail for a container given options
|
||||||
@ -164,12 +166,17 @@ func getTailLog(path string, tail int) ([]*LogLine, error) {
|
|||||||
func (l *LogLine) String(options *LogOptions) string {
|
func (l *LogLine) String(options *LogOptions) string {
|
||||||
var out string
|
var out string
|
||||||
if options.Multi {
|
if options.Multi {
|
||||||
|
if options.UseName {
|
||||||
|
cname := l.CName
|
||||||
|
out = fmt.Sprintf("%s ", cname)
|
||||||
|
} else {
|
||||||
cid := l.CID
|
cid := l.CID
|
||||||
if len(cid) > 12 {
|
if len(cid) > 12 {
|
||||||
cid = cid[:12]
|
cid = cid[:12]
|
||||||
}
|
}
|
||||||
out = fmt.Sprintf("%s ", cid)
|
out = fmt.Sprintf("%s ", cid)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if options.Timestamps {
|
if options.Timestamps {
|
||||||
out += fmt.Sprintf("%s ", l.Time.Format(LogTimeFormat))
|
out += fmt.Sprintf("%s ", l.Time.Format(LogTimeFormat))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user