question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

use pythonic way to read log

See original GitHub issue

I was trying to run this with WSL to see if that would work. It seems like there is an issue where you can’t follow a file in WSL without the ---disable-inotify flag on tail. I tested this and added that option in the log consumer and it does work after that. I noticed there have been issues with Windows as well during log rotations. I’m not sure if switching over to a pythonic way of reading the debug file will solve this, but it seems like it would be better since you don’t need differentiate between OSes either.

Not sure if remote would work with this either, so it would require a bit of testing.

https://github.com/martomi/chiadog/blob/da1a904466e6df25b930d58cefb70eca6dce4a3a/src/chia_log/log_consumer.py#L72-L80

Maybe something like: https://stackoverflow.com/questions/12523044/how-can-i-tail-a-log-file-in-python/53121178#53121178

Edit:

Initially something like this appears to work with little resources:

    def _consume_loop(self):
        expanded_user_log_path = self._log_path.expanduser()
        logging.info(f"Consuming log file from {expanded_user_log_path}")

        try:
            fp = open(expanded_user_log_path, "r")
            st_results = os.stat(expanded_user_log_path)
            st_size = st_results[6]
            fp.seek(st_size)

            while self._is_running:
                where = fp.tell()
                line = fp.readline()
                if not line:
                    time.sleep(1)
                    fp.seek(where)
                elif fp.tell() > os.path.getsize(expanded_user_log_path):
                    logging.info("Log rotated, opening latest log file")
                    fp.close()
                    fp = open(expanded_user_log_path, "r")
                    fp.seek(0,2)
                else:
                    self._notify_subscribers(line)
        except Exception as e:
            fp.close()
            logging.critical(f"Error consuming log file: {e}")

I can run it for a bit and see what happens during rotations as well. This seems to work the same for windows.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pieterhelsencommented, May 11, 2021

Definitely worth a try and would be nice if it worked cross-platform. My work on the log rotation issue is available here: https://github.com/martomi/chiadog/tree/windows-rotation

It’s tested on Linux local, Linux remote and Windows local, but I can’t test Windows remote without setting up a dummy node

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python read log files and get lines containing specific words
Python read log files and get lines containing specific words - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing ...
Read more >
Logging HOWTO — Python 3.11.1 documentation
To log variable data, use a format string for the event description message and append the variable data as arguments. For example: import...
Read more >
Logging in Python - Real Python
Logging is a very useful tool in a programmer's toolbox. It can help you develop a better understanding of the flow of a...
Read more >
Log File Analysis: Python Read file, line & Read from file
In this video, we will see python searches in a very simple way. Python is a great utility to do this type of...
Read more >
How To Do Logging in Python. Tracking Events In Your Program
In Python, you can use the logging module to create logs. It's a popular Python built-in that's used by most third-party libraries, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found