Discussion:
[bottlepy] Bottle logging idea/design help
Jeff Eberl
2015-01-10 01:36:25 UTC
Permalink
I want to log events from the python logging library to server sent events
in the web page. I have a working example, but I'm sure I'm doing some
things wrong, and I'd like to expand it for others to use.

Here's what I'm doing (part of my thermostat project, but I will be
releasing it separately if it ever works):
https://github.com/jeffeb3/YunThermostat/blob/master/linux/sseLogHandler.py

I've made a new logging handler that inherits from
logging.handlers.QueueHandler. (sidenote, python < 3.2 doesn't have it, so
I'm inheriting from logutils)

logs get put in the queue, then a text/event-stream callback reads each
item from the queue, formats it, and yields that data to the client.

Normal sse stuff is happening on the client side, to make an EventServer to
connect to "/logs", and append that text to the log div on the web page.

Since the handler is it's own class, I have to connect it to bottle
somehow, and the best way I've come up with is to send in a Bottle(), or
don't send anything, and the handler will make a Bottle and run it in a
thread.

Thanks for reading, here's where I need help:

1) I'm not sure how to best integrate with Bottle. Should I be writing this
as a plugin? I saw something in the API about load_module, should I be
learning about that (is there a good example)? I'm imaging two use cases.
One is an existing bottle web server, such as mine, and one would be a dumb
python app without a web server, and the handler would run the server for
me.

2) I'm using a queue, which is not ideal, because if I have two clients,
the data is split between the consumers. I am hoping for a replacement for
queue, something that will let me have X items stored in memory, and when a
client connects, they will get that history, and then start getting new
events. I can do it The Hard Way by making a lock on some objects, and keep
a deque with X items in it, and manage the locks myself, but I'm hoping
someone out here knows a better module for that.

In my particular application, I can't use gevents, because I'm deploying to
a Yun, which is openWrt, and it's not available.

Thanks.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...