Browse code

Fail docker logs on all logging drivers apart from 'json-file'

Signed-off-by: Alexander Morozov <lk4d4@docker.com>

Alexander Morozov authored on 2015/02/06 09:24:47
Showing 11 changed files
... ...
@@ -1929,6 +1929,10 @@ func (cli *DockerCli) CmdLogs(args ...string) error {
1929 1929
 		return err
1930 1930
 	}
1931 1931
 
1932
+	if env.GetSubEnv("HostConfig").GetSubEnv("LogConfig").Get("Type") != "json-file" {
1933
+		return fmt.Errorf("\"logs\" command is supported only for \"json-file\" logging driver")
1934
+	}
1935
+
1932 1936
 	v := url.Values{}
1933 1937
 	v.Set("stdout", "1")
1934 1938
 	v.Set("stderr", "1")
... ...
@@ -1489,3 +1489,12 @@ func (container *Container) getNetworkedContainer() (*Container, error) {
1489 1489
 func (container *Container) Stats() (*execdriver.ResourceStats, error) {
1490 1490
 	return container.daemon.Stats(container)
1491 1491
 }
1492
+
1493
+func (c *Container) LogDriverType() string {
1494
+	c.Lock()
1495
+	defer c.Unlock()
1496
+	if c.hostConfig.LogConfig.Type == "" {
1497
+		return c.daemon.defaultLogConfig.Type
1498
+	}
1499
+	return c.hostConfig.LogConfig.Type
1500
+}
... ...
@@ -44,6 +44,9 @@ func (daemon *Daemon) ContainerLogs(job *engine.Job) engine.Status {
44 44
 	if err != nil {
45 45
 		return job.Error(err)
46 46
 	}
47
+	if container.LogDriverType() != "json-file" {
48
+		return job.Errorf("\"logs\" endpoint is supported only for \"json-file\" logging driver")
49
+	}
47 50
 	cLog, err := container.ReadLog("json")
48 51
 	if err != nil && os.IsNotExist(err) {
49 52
 		// Legacy logs
... ...
@@ -26,6 +26,7 @@ docker-create - Create a new container
26 26
 [**--ipc**[=*IPC*]]
27 27
 [**--link**[=*[]*]]
28 28
 [**--lxc-conf**[=*[]*]]
29
+[**--log-driver**[=*[]*]]
29 30
 [**-m**|**--memory**[=*MEMORY*]]
30 31
 [**--memory-swap**[=*MEMORY-SWAP*]]
31 32
 [**--mac-address**[=*MAC-ADDRESS*]]
... ...
@@ -108,6 +109,10 @@ IMAGE [COMMAND] [ARG...]
108 108
 **--lxc-conf**=[]
109 109
    (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"
110 110
 
111
+**--log-driver**="|*json-file*|*none*"
112
+  Logging driver for container. Default is defined by daemon `--log-driver` flag.
113
+  **Warning**: `docker logs` command works only for `json-file` logging driver.
114
+
111 115
 **-m**, **--memory**=""
112 116
    Memory limit (format: <number><optional unit>, where unit = b, k, m or g)
113 117
 
... ...
@@ -22,6 +22,8 @@ The **docker logs --follow** command combines commands **docker logs** and
22 22
 **docker attach**. It will first return all logs from the beginning and
23 23
 then continue streaming new output from the container’s stdout and stderr.
24 24
 
25
+**Warning**: This command works only for **json-file** logging driver.
26
+
25 27
 # OPTIONS
26 28
 **--help**
27 29
   Print usage statement
... ...
@@ -27,6 +27,7 @@ docker-run - Run a command in a new container
27 27
 [**--ipc**[=*IPC*]]
28 28
 [**--link**[=*[]*]]
29 29
 [**--lxc-conf**[=*[]*]]
30
+[**--log-driver**[=*[]*]]
30 31
 [**-m**|**--memory**[=*MEMORY*]]
31 32
 [**--memory-swap**[=*MEMORY-SWAP]]
32 33
 [**--mac-address**[=*MAC-ADDRESS*]]
... ...
@@ -209,6 +210,10 @@ which interface and port to use.
209 209
 **--lxc-conf**=[]
210 210
    (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"
211 211
 
212
+**--log-driver**="|*json-file*|*none*"
213
+  Logging driver for container. Default is defined by daemon `--log-driver` flag.
214
+  **Warning**: `docker logs` command works only for `json-file` logging driver.
215
+
212 216
 **-m**, **--memory**=""
213 217
    Memory limit (format: <number><optional unit>, where unit = b, k, m or g)
214 218
 
... ...
@@ -82,6 +82,10 @@ unix://[/path/to/socket] to use.
82 82
 **--label**="[]"
83 83
   Set key=value labels to the daemon (displayed in `docker info`)
84 84
 
85
+**--log-driver**="*json-file*|*none*"
86
+  Container's logging driver. Default is `default`.
87
+  **Warning**: `docker logs` command works only for `json-file` logging driver.
88
+
85 89
 **--mtu**=VALUE
86 90
   Set the containers network mtu. Default is `1500`.
87 91
 
... ...
@@ -252,6 +252,7 @@ Json Parameters:
252 252
   -   **LogConfig** - Logging configuration to container, format
253 253
         `{ "Type": "<driver_name>", "Config": {"key1": "val1"}}
254 254
         Available types: `json-file`, `none`.
255
+        `json-file` logging driver.
255 256
 
256 257
 Query Parameters:
257 258
 
... ...
@@ -441,6 +442,9 @@ Status Codes:
441 441
 
442 442
 Get stdout and stderr logs from the container ``id``
443 443
 
444
+> **Note**:
445
+> This endpoint works only for containers with `json-file` logging driver.
446
+
444 447
 **Example request**:
445 448
 
446 449
        GET /containers/4fa6e0f0c678/logs?stderr=1&stdout=1&timestamps=1&follow=1&tail=10 HTTP/1.1
... ...
@@ -1405,6 +1405,9 @@ For example:
1405 1405
       -t, --timestamps=false    Show timestamps
1406 1406
       --tail="all"              Number of lines to show from the end of the logs
1407 1407
 
1408
+NOTE: this command is available only for containers with `json-file` logging
1409
+driver.
1410
+
1408 1411
 The `docker logs` command batch-retrieves logs present at the time of execution.
1409 1412
 
1410 1413
 The `docker logs --follow` command will continue streaming the new output from
... ...
@@ -565,13 +565,15 @@ familiar with using LXC directly.
565 565
 
566 566
 You can specify a different logging driver for the container than for the daemon.
567 567
 
568
-### Log driver: none
568
+### Logging driver: none
569 569
 
570
-Disables any logging for the container.
570
+Disables any logging for the container. `docker logs` won't be available with
571
+this driver.
571 572
 
572 573
 ### Log driver: json-file
573 574
 
574
-Default logging driver for Docker. Writes JSON messages to file.
575
+Default logging driver for Docker. Writes JSON messages to file. `docker logs`
576
+command is available only for this logging driver
575 577
 
576 578
 ## Overriding Dockerfile image defaults
577 579
 
... ...
@@ -703,3 +703,26 @@ func TestDaemonLoggingDriverNoneOverride(t *testing.T) {
703 703
 	}
704 704
 	logDone("daemon - 'none' logging driver override in run")
705 705
 }
706
+
707
+func TestDaemonLoggingDriverNoneLogsError(t *testing.T) {
708
+	d := NewDaemon(t)
709
+
710
+	if err := d.StartWithBusybox("--log-driver=none"); err != nil {
711
+		t.Fatal(err)
712
+	}
713
+	defer d.Stop()
714
+
715
+	out, err := d.Cmd("run", "-d", "busybox", "echo", "testline")
716
+	if err != nil {
717
+		t.Fatal(out, err)
718
+	}
719
+	id := strings.TrimSpace(out)
720
+	out, err = d.Cmd("logs", id)
721
+	if err == nil {
722
+		t.Fatalf("Logs should fail with \"none\" driver")
723
+	}
724
+	if !strings.Contains(out, `\"logs\" command is supported only for \"json-file\" logging driver`) {
725
+		t.Fatalf("There should be error about non-json-file driver, got %s", out)
726
+	}
727
+	logDone("daemon - logs not available for non-json-file drivers")
728
+}