lopes
  • Home
  • Categories
  • Tags
  • Octet
  • About

>> Home / technology / bits, python, logs

Bits #1: Python, syslog, macOS

∵ José Lopes ∴ 2021-08-17 ∞ 2'

It's been a while since I posted for the last time, but today I decided to start a new session in this blog: Bits. Here, I'm going to share short, but good and useful experiences to help other people, including myself [in the future]. To debut the new session, I will describe the steps I took to send messages from a Python script to Syslog and how to monitor those logs in macOS.

Well, Python (3.9) comes with the Syslog module, which handles the process of creating log entries and it is really simple and straightforward. For instance, the line below creates a critical Syslog message:

import syslog
syslog.syslog(syslog.LOG_CRIT, 'hello, syslog!')

In my experience, I had to run Python in unbuffered mode passing the -u option to the interpreter. Then, to check log files, I noticed that macOS does not work with Syslog like other similar systems. It comes with the log utility that has some commands to work with logs. Using the stream command with the right parameters and filtering the Python process, I was able to see all logs coming in [almost] real-time when the script was executed (almost like in tail -f).

log stream --info --debug --predicate "process == 'Python'"

And that's it! Logging like a boss! :)

References

  • Stack Exchange
  • Python: Syslog

Search

Categories

  • life
  • technology

Tags

  • arch
  • beer
  • bits
  • certification
  • cloud
  • dev
  • git
  • linux
  • logs
  • oss
  • python
  • review
  • rust
  • security
  • zola

2020 © José Lopes | Twitter Linkedin GitHub | Built on Zola