# Redirect logs to stdout

**URL:** <https://community.fly.io/t/redirect-logs-to-stdout/514>\
**Category:** General\
**Created:** [January 1, 2021, 9:20pm UTC](https://community.fly.io/t/redirect-logs-to-stdout/514 "2021-01-01T21:20:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jveres](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jveres/32/271_2.png) [@jveres](https://community.fly.io/u/jveres)\
**Post date:** [January 1, 2021, 9:20pm UTC](https://community.fly.io/t/redirect-logs-to-stdout/514/1 "2021-01-01T21:20:40Z")

</div>

Hi! I’m using `/proc/1/fd/1` in Docker to redirect some file based logs to the standard output to avoid `sh` and `tail` etc. Is there maybe a similarly simple solution in a fly VM too because it doesn’t seem to work… thx.

---

<div class="post-metadata">

**Author:** ![jerome](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jerome/32/17_2.png) [@jerome](https://community.fly.io/u/jerome)\
**Post date:** [January 1, 2021, 10:28pm UTC](https://community.fly.io/t/redirect-logs-to-stdout/514/2 "2021-01-01T22:28:18Z")

</div>

In a Fly virtual machine, PID 1 is not the process spawned by your CMD. It’s our custom init program that manages a few things.

You should be able to use /dev/stdout and /dev/stderr respectively though.

If not, maybe /proc/self/fd/1? I’m not sure.

---

<div class="post-metadata">

**Author:** ![jveres](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jveres/32/271_2.png) [@jveres](https://community.fly.io/u/jveres)\
**Post date:** [January 1, 2021, 11:12pm UTC](https://community.fly.io/t/redirect-logs-to-stdout/514/3 "2021-01-01T23:12:19Z")

</div>

Actually none of those work, I tried them all.

---

<div class="post-metadata">

**Author:** ![jerome](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jerome/32/17_2.png) [@jerome](https://community.fly.io/u/jerome)\
**Post date:** [January 2, 2021, 12:08am UTC](https://community.fly.io/t/redirect-logs-to-stdout/514/4 "2021-01-02T00:08:19Z")

</div>

I’m not entirely sure I understand what you’re trying to do 🤔

Would you mind sharing some of these scripts? stdout and stderr should _just work_, including redirections. I think I might be able to help if I have a more concrete example.

---

<div class="post-metadata">

**Author:** ![jveres](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jveres/32/271_2.png) [@jveres](https://community.fly.io/u/jveres)\
**Post date:** [January 2, 2021, 9:31am UTC](https://community.fly.io/t/redirect-logs-to-stdout/514/5 "2021-01-02T09:31:41Z")

</div>

Sure, I was expecting `echo "Hello" > /dev/console` to produce a log entry in `fly logs`.  
I would like to use it in a container created from scratch image where an app expects a file as `-logfile xxx` parameter to log into. Passing `-logfile /proc/1/fd/1` works, also `-logfile /dev/console` works with `docker run` but not with `fly logs` after deployment. I understand the PID 1 is a custom init code in fly. Is logging into `/dev/console` expected to work with fly?

---

<div class="post-metadata">

**Author:** ![jerome](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jerome/32/17_2.png) [@jerome](https://community.fly.io/u/jerome)\
**Post date:** [January 2, 2021, 12:03pm UTC](https://community.fly.io/t/redirect-logs-to-stdout/514/6 "2021-01-02T12:03:54Z")

</div>

> [@jveres](#):
>
> Is logging into `/dev/console` expected to work with fly?

It should work yes. I just tried it from my vscode remote (ssh) app:

```auto
$ sudo su -
# echo "Hello" > /dev/console

```

```auto
2021-01-02T11:39:44.984Z c77b8f67 ewr [info] Hello

```

You do need root permissions to write to `/dev/console`.

Our virtual machines are configured with a serial tty, so you can also write to `/dev/ttyS0` (but you still need root access).

Your main’s program’s (`<ENTRYPOINT> <CMD>`) stdout and stderr are inherited from the parent process (PID 1, our init). If your app sends its logs to `/dev/stdout` and such, it should just work.

---

<div class="post-metadata">

**Author:** ![jveres](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jveres/32/271_2.png) [@jveres](https://community.fly.io/u/jveres)\
**Post date:** [January 2, 2021, 12:37pm UTC](https://community.fly.io/t/redirect-logs-to-stdout/514/7 "2021-01-02T12:37:03Z")

</div>

> [@jerome](#):
>
> You do need root permissions to write to `/dev/console` .

Thanks, I think it will be then a permission issue on my side: I cannot let the app run as root.

---

<div class="post-metadata">

**Author:** ![jveres](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jveres/32/271_2.png) [@jveres](https://community.fly.io/u/jveres)\
**Post date:** [January 2, 2021, 1:07pm UTC](https://community.fly.io/t/redirect-logs-to-stdout/514/8 "2021-01-02T13:07:12Z")

</div>

Thanks @jerome, if I give root permission the app can indeed log into `/dev/console` in the fly VM as well.

---

<div class="post-metadata">

**Author:** ![jerome](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jerome/32/17_2.png) [@jerome](https://community.fly.io/u/jerome)\
**Post date:** [January 2, 2021, 1:08pm UTC](https://community.fly.io/t/redirect-logs-to-stdout/514/9 "2021-01-02T13:08:27Z")

</div>

I expect you should be fine if the stdout redirect chain is not broken. If you provide your full dockerfile, entrypoint and everything relevant, I’m sure we can make it work without root!

We do not encourage the use of the root user.

---

<div class="post-metadata">

**Author:** ![jveres](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jveres/32/271_2.png) [@jveres](https://community.fly.io/u/jveres)\
**Post date:** [January 2, 2021, 1:28pm UTC](https://community.fly.io/t/redirect-logs-to-stdout/514/10 "2021-01-02T13:28:59Z")

</div>

Not so critical for me now but would be safer without root, we agree on that.  
I appreciate if you find a solution to use [this](https://github.com/jveres/althttpd-xsim/blob/main/Dockerfile) image without root. Thanks!

---

<div class="post-metadata">

**Author:** ![jerome](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jerome/32/17_2.png) [@jerome](https://community.fly.io/u/jerome)\
**Post date:** [January 3, 2021, 11:30am UTC](https://community.fly.io/t/redirect-logs-to-stdout/514/11 "2021-01-03T11:30:01Z")

</div>

I think I found a bug in our init, preventing access to the ultimate destination of `/dev/stdout` for spawned processes.

Will update here once I’ve fixed it.

---

<div class="post-metadata">

**Author:** ![jerome](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jerome/32/17_2.png) [@jerome](https://community.fly.io/u/jerome)\
**Post date:** [January 3, 2021, 2:13pm UTC](https://community.fly.io/t/redirect-logs-to-stdout/514/12 "2021-01-03T14:13:30Z")

</div>

@jveres I’ve pushed out a fix.

Essentially, `/dev/stdout` pointed at `/proc/self/fd/1` which itself pointed at `/dev/console` which requires root to read/write from/to (as we’ve established before).

I switched that up to launch the child process with a `pipe` for both stdout and stderr. I also `chown`ed the pipe’s file descriptor to the proper uid and gid (specified from the docker image, or else root).

Unfortunately, `althttpd` still won’t log there in my tests. I’m using your initial commit which doesn’t require superuser permissions. I’ve tested that it works by adding an `ENTRYPOINT` which echoed to the `/dev/stdout` pipe: without my fix, this didn’t work (permission denied).

So I’m thinking this could now be a bug in althttpd and symlinks? I tried running it with `-logfile /dev/stdout` and `-logfile /proc/self/fd/1` and it did not work, even from docker (it didn’t log anything when making a request to it). How did you get it running with docker? Does it not log requests? What request are you issuing it to test this?

Hope this helps! Feel free to re-deploy to try it out.

---

<div class="post-metadata">

**Author:** ![jveres](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jveres/32/271_2.png) [@jveres](https://community.fly.io/u/jveres)\
**Post date:** [January 3, 2021, 6:22pm UTC](https://community.fly.io/t/redirect-logs-to-stdout/514/13 "2021-01-03T18:22:10Z")

</div>

`althttpd` is capable of running from `xinetd` and sends its output to the standard output.  
Using `-logfile /dev/stderr` works with non-privileged user now, thanks a lot @jerome
