Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging to /dev/stdout and to file at the same time. #252

Closed
markmadlangbayan opened this issue Feb 18, 2019 · 7 comments
Closed

Logging to /dev/stdout and to file at the same time. #252

markmadlangbayan opened this issue Feb 18, 2019 · 7 comments

Comments

@markmadlangbayan
Copy link

Hello,

Is it possible to log to /dev/stdout and the same time to a file in /var/log directory? I tried to redirect all longs to /dev/stdout, and I can actually see the logs as output in docker, but not in a file. Then I read the documentation, and used the logutil-service by creating a file called log/run. Now it redirected all outputs from screen to the file in /var/log/*/current file.

I'd like to set it up where I see the outputs on screen, and also in file.

Thanks,
Mark

@jprjr
Copy link
Member

jprjr commented Feb 19, 2019

I don't think there's an easy way to do it using logutil-service (it only reads in one argument), but if you use s6-log directly you can do it.

Example Dockerfile

FROM ubuntu:16.04
ADD https://github.com/just-containers/s6-overlay/releases/download/v1.21.8.0/s6-overlay-amd64.tar.gz /tmp/
RUN tar xzf /tmp/s6-overlay-amd64.tar.gz -C /

COPY root /

ENTRYPOINT ["/init"]

Contents of root folder

jprjr test $ tree root
root
└── etc
    ├── cont-init.d
    │   └── setup-someservice-logdirs
    └── services.d
        └── some-service
            ├── log
            │   └── run
            └── run

5 directories, 3 files

Contents of root/etc/cont-init.d/setup-someservice-logdirs

#!/usr/bin/env bash
set -e

mkdir -p /var/log/some-service
chown nobody:nogroup /var/log/some-service

Contents of root/etc/services.d/some-service/run

#!/usr/bin/env bash

while true ; do
  sleep 1
  echo "Some text"
done

Contents of root/etc/services.d/some-service/log/run

#!/usr/bin/execlineb

s6-envuidgid nobody
s6-applyuidgid -U
s6-log -bp T 1 n20 s1000000 T /var/log/some-service

The log will be written to both /var/log/some-service as well as standard output.

The key is that s6-log reads in a whole "logging script" on the command line, and you can have multiple actions - here's the link to the manual.

Here's the breakdown of what I'm doing in my s6-log command:

  • s6-log -bp - call s6-log in blocking, protected mode (same as logutil-service does)
  • T 1 -- this is my first action directive
    • take all lines of input (because I don't have any line selection directives)
    • prepend with an ISO 8601 timestamp (done with T)
    • forward to stdout (done with the 1)
  • n20 s1000000 T /var/log/some-service -- this is second first action directive, and it's the default mode for logutil-service
    • take all lines of input (again, no selection directives)
    • keep up to 20 files (n20)
    • each file is max 1000000 bytes (s1000000)
    • prepend with an ISO 8601 timestamp (T)
    • save to logdir /var/log/some-service

@markmadlangbayan
Copy link
Author

Thank you for the detailed response, it worked!

@felipecrs
Copy link

Any plans to add an option, or environment variable, to allow logutil-service to abstract this configuration?

@skarnet
Copy link
Contributor

skarnet commented Oct 24, 2022

S6_LOGGING_SCRIPT?

@felipecrs
Copy link

Got it. Now I understand, to retain the behavior described above, I suppose I would have to set:

S6_LOGGING_SCRIPT="T 1 n20 s1000000 T"

Is this correct?

@skarnet
Copy link
Contributor

skarnet commented Oct 24, 2022

Something like that, yes. :-)

@felipecrs
Copy link

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants