Saving logs?

Is there an option to save logs?
There is an option to use loggings inside as a write on the screen.
But when trying to save logs, it does not work, it does not create the logging file.

Hi @BugzTheBunny,

Thank you for sharing with the Streamlit community!

This post covers how to enable logging via the command line. You mentioned that you have already tried to save logs – can you provide a code snippet or screenshot of the commands you’ve already tried?

Best,

Caroline

1 Like

Hi @Caroline !

The code is pretty simple:

import streamlit as st
import logging

logging.basicConfig(filename='example.log')
logging.debug('This message should go to the log file')

But for some reason the exmaple.log file is not being created, and u did run it now using
streamlit run app.py --logger.level=debug

1 Like

Hi @BugzTheBunny,

You will need to add a parameter to what you’re entering in the command line that will redirect stderr to a file. The syntax of this parameter should be “2>logfilename.txt”

For example:

streamlit run streamlit_app.py --logger.level=debug 2>logs.txt

Best,

Caroline

2 Likes

Okay, that’s great for a run command, is there an option to make it in a config file?

Thanks for the answer!!

Hi @BugzTheBunny,

Yes, please check out our documentation on config files here

Best,

Caroline

2 Likes

Hey!

Thanks again, really helpful.
But I actually meant is there an option to use the ‘2> logs.log’ into the config?
If not that’s also okay, I’m using docker, so that’s not really an issue, just wanted to know :slight_smile:

Hi @BugzTheBunny,

That piece is just redirecting stderr to a different file, so you would need to include it in the command unfortunately.

Best,

Caroline

1 Like

Hi again… this is not working when using docker :confused:
I run the code, but while using docker for this, it does not work when using docker… :confused:
the output is still being seen in the console, rathar then being writtenin the file…
beyond that, the file itself is not being created.

DockerFile:

FROM python:3.8-slim-buster
COPY streamlit_src /app
COPY aoi_defects_clustering/src /app/src
COPY unit_level_analysis /app/unit_level_analysis
COPY machines.csv /app/machines.csv
WORKDIR /app
RUN python -m pip install --upgrade pip
RUN pip install -r requirements.txt
EXPOSE 8889
ENTRYPOINT ["streamlit","run","--server.port","8889","app.py","--logger.level=info","2>","streamlit_logs.log"]

Am I missing something?

1 Like

Found a solution!

FROM python:3.8-slim-buster

COPY streamlit_src /app

...
...

COPY run_app.sh /app/run_app.sh

RUN python -m pip install --upgrade pip

RUN pip install -r requirements.txt

RUN chmod +x /app/run_app.sh

WORKDIR /app

EXPOSE 8889

ENTRYPOINT ["/bin/bash"]

CMD ["./run_app.sh"]

run_app.sh:
streamlit run --server.port 8889 app.py --logger.level=info 2> streamlit_logs.log

3 Likes

@Caroline

Nonetheless, it would be a nice feature if we could also enable file logging in config.toml besides the log level :wink:

1 Like

@BugzTheBunny

A small suggestion for improving your Dockerfile:

WORKDIR /app
COPY . .

and add a .dockerignore file to the repo so that only the wanted files get copied to the container image.

1 Like

Will COPY . . just copy all of the content? if so, that’s not what i need unfortunately, there are a lot of items i need to ignore, si this is a shorter way.

Or im missing something?

Yes.
But you can use also inverted logic in the ignore files, for example

# Ignore everything
*

# But not these files...
!file1.py
!file2.py
1 Like

Great to know!
Im pretty new to docker, been using it for about a month only, and would not really call myself a poweruser of this.

Will use it in the future, but at this point the shorter way is just to copy these 4 files :smiley:

Thanks a lot tho!

As far as i know, the .dockerignore uses the same syntax/logic as the .gitignore files.
So you can use this knowledge in different cases.

1 Like

Will dig deeper then

Hi @Franky1,

That makes sense – feel free to submit a feature request here.

Best,

Caroline

(10 months later) :slight_smile:

I have a similar, but slightly different problem.
Running my app locally, I can download (from within the app) a log file containing all the log-messages.

However, when I run the hosted app, I see the logs only in the little “manage app”-shell.
The file that I download is always empty!

Any help would be appreciated.

Here are some screenshots and a link to the app:

Hosted

Local

app

I suspected that logging’s config is not correct.
So, just for the sake of debugging things, I tried a new, easy to config library called lovely-logger.
Locally, everything works fine, but this time the online version produces a file with duplicated messages

Any idea what might be misconfigured?

I tried to configure the logger

In a function, that is called only once per session.

Thanks for your help!