Improve service logs

This commit is contained in:
cuigh 2017-09-29 16:43:02 +08:00
parent 6ce9d3358e
commit 4a549e156f
6 changed files with 49 additions and 43 deletions

View File

@ -245,6 +245,14 @@ dd {
border-top-right-radius: 0 !important; border-top-right-radius: 0 !important;
} }
.code {
font-family: monospace;
}
textarea.code{
padding: 0.75em;
}
.CodeMirror { .CodeMirror {
font-size: 0.8em; font-size: 0.8em;
border: 1px solid #ddd; border: 1px solid #ddd;

View File

@ -1,19 +1,15 @@
package docker package docker
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"io"
"sort" "sort"
"strconv"
"strings" "strings"
"time" "time"
"io"
"bufio"
"bytes"
"strconv"
"github.com/cuigh/swirl/misc" "github.com/cuigh/swirl/misc"
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
@ -21,6 +17,7 @@ import (
"github.com/docker/docker/api/types/mount" "github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
) )
// ServiceList return service list. // ServiceList return service list.
@ -523,37 +520,30 @@ func ServiceRemove(name string) error {
// ServiceLogs returns the logs generated by a service. // ServiceLogs returns the logs generated by a service.
func ServiceLogs(name string, line int, timestamps bool) (stdout, stderr *bytes.Buffer, err error) { func ServiceLogs(name string, line int, timestamps bool) (stdout, stderr *bytes.Buffer, err error) {
err = mgr.Do(func(ctx context.Context, cli *client.Client) (err error) { var (
var ( ctx context.Context
rc io.ReadCloser cli *client.Client
buf []byte rc io.ReadCloser
) )
opts := types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Tail: strconv.Itoa(line),
Timestamps: timestamps,
//Since: (time.Hour * 24).String()
}
if rc, err = cli.ServiceLogs(ctx, name, opts); err == nil {
defer rc.Close()
stdout = &bytes.Buffer{} ctx, cli, err = mgr.Client()
stderr = &bytes.Buffer{} if err != nil {
scanner := bufio.NewScanner(rc)
for scanner.Scan() {
buf = scanner.Bytes()
if buf[0] == 1 {
stdout.Write(buf[8:])
stdout.WriteByte('\n')
} else if buf[0] == 2 {
stdout.Write(buf[8:])
stdout.WriteByte('\n')
}
}
err = scanner.Err()
}
return return
}) }
opts := types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Tail: strconv.Itoa(line),
Timestamps: timestamps,
//Since: (time.Hour * 24).String()
}
if rc, err = cli.ServiceLogs(ctx, name, opts); err == nil {
defer rc.Close()
stdout = &bytes.Buffer{}
stderr = &bytes.Buffer{}
_, err = stdcopy.StdCopy(stdout, stderr, rc)
}
return return
} }

View File

@ -20,6 +20,6 @@ type Setting struct {
UpdatedAt time.Time `bson:"updated_at" json:"updated_at,omitempty"` UpdatedAt time.Time `bson:"updated_at" json:"updated_at,omitempty"`
} }
func Test() { func Test() {
time.Now().Zone() time.Now().Zone()
} }

View File

@ -4,7 +4,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="/lyicon/css/lyicon.css"> <link rel="stylesheet" href="/lyicon/css/lyicon.css">
<link rel="stylesheet" href="/bulma/bulma.css?v=0.5.2"> <link rel="stylesheet" href="/bulma/bulma.css?v=0.5.3">
<link rel="stylesheet" href="/swirl/css/swirl.css?v={{ version }}"> <link rel="stylesheet" href="/swirl/css/swirl.css?v={{ version }}">
{{ block style() }}{{end}} {{ block style() }}{{end}}
</head> </head>

View File

@ -4,7 +4,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="/lyicon/css/lyicon.css"> <link rel="stylesheet" href="/lyicon/css/lyicon.css">
<link rel="stylesheet" href="/bulma/bulma.css?v=0.5.2"> <link rel="stylesheet" href="/bulma/bulma.css?v=0.5.3">
<link rel="stylesheet" href="/swirl/css/swirl.css?v={{ version }}"> <link rel="stylesheet" href="/swirl/css/swirl.css?v={{ version }}">
</head> </head>
<body> <body>

View File

@ -77,7 +77,7 @@
</form> </form>
</nav> </nav>
<div class="tabs is-toggle is-fullwidth is-marginless" data-target="tab-content"> <div class="tabs is-boxed" data-target="tab-content">
<ul> <ul>
<li class="is-active"> <li class="is-active">
<a><span>Stdout</span></a> <a><span>Stdout</span></a>
@ -88,8 +88,16 @@
</ul> </ul>
</div> </div>
<div id="tab-content" class="content"> <div id="tab-content" class="content">
<pre class="is-marginless" style="max-height: 600px; padding: 0.75em">{{ .Stdout }}</pre> <div class="field">
<pre class="is-marginless" style="max-height: 600px; padding: 0.75em; display: none">{{ .Stderr }}</pre> <div class="control">
{{ if .Stdout }}<textarea class="textarea code is-small" rows="30" readonly>{{ .Stdout }}</textarea>{{ end }}
</div>
</div>
<div class="field" style="display: none">
<div class="control">
{{ if .Stderr }}<textarea class="textarea code is-small" rows="30" readonly>{{ .Stderr }}</textarea>{{ end }}
</div>
</div>
</div> </div>
<a href="/service/" class="button is-primary"> <a href="/service/" class="button is-primary">
<span class="icon"><i class="fa fa-reply"></i></span> <span class="icon"><i class="fa fa-reply"></i></span>