Comments (8)
So I think you can use the snippet from the Logging Cookbook: Sending and receiving logging events across a network.
That would look like this:
# server.py
import time
import socketserver
import threading
import pickle
import struct
from loguru import logger
class LogRecordStreamHandler(socketserver.StreamRequestHandler):
def handle(self):
while True:
chunk = self.connection.recv(4)
if len(chunk) < 4:
break
slen = struct.unpack('>L', chunk)[0]
chunk = self.connection.recv(slen)
while len(chunk) < slen:
chunk = chunk + self.connection.recv(slen - len(chunk))
record = pickle.loads(chunk)
logger.opt(raw=True).log(record['levelno'], record['msg'] + '\n')
address = ('localhost', 9999)
server = socketserver.TCPServer(address, LogRecordStreamHandler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
while 1:
time.sleep(1)
logger.info("Message from server")
# client.py
import time
from loguru import logger
from logging.handlers import SocketHandler
socket_handler = SocketHandler('localhost', 9999)
logger.remove()
logger.add(socket_handler)
while 1:
time.sleep(1)
logger.info("Message from client")
I tested it and it seems to work, but this is not as elegant as I would like. Ideally, you would just log the unpickled record, so that formatting depends on your server (and not client) handlers. Keep in mind that pickle
is unsafe, so you maybe better should serialize the logs using JSON.
from loguru.
Oh, yes you're correct, thanks for pointing this out! The above snippet was a "translation" of the existing logging
cookbook, but it needs to be adapted to accept multiple clients at the same time, indeed.
from loguru.
Hi @popeaaa.
You can log to the same file from different modules (i.e. Python files composing your application), just call .add()
only once, the imported logger
is the same object so share the same handlers.
Do you mean you have two applications run independently and executed by two different processes?
Well, if you .add("file.log")
from the two apps, it risks to produce corrupted logs obviously. So you need some kind of communication for synchronization between the two processes. Maybe using the built-in SocketHandler
with one server (writing logs to file) and one client (sending logs to server)?
from loguru.
Hi @popeaaa.
You can log to the same file from different modules (i.e. Python files composing your application), just call
.add()
only once, the importedlogger
is the same object so share the same handlers.Do you mean you have two applications run independently and executed by two different processes?
Well, if you
.add("file.log")
from the two apps, it risks to produce corrupted logs obviously. So you need some kind of communication for synchronization between the two processes. Maybe using the built-inSocketHandler
with one server (writing logs to file) and one client (sending logs to server)?
thank you ,it is 2 processes
from loguru.
So I think you can use the snippet from the Logging Cookbook: Sending and receiving logging events across a network.
That would look like this:
# server.py import time import socketserver import threading import pickle import struct from loguru import logger class LogRecordStreamHandler(socketserver.StreamRequestHandler): def handle(self): while True: chunk = self.connection.recv(4) if len(chunk) < 4: break slen = struct.unpack('>L', chunk)[0] chunk = self.connection.recv(slen) while len(chunk) < slen: chunk = chunk + self.connection.recv(slen - len(chunk)) record = pickle.loads(chunk) logger.opt(raw=True).log(record['levelno'], record['msg'] + '\n') address = ('localhost', 9999) server = socketserver.TCPServer(address, LogRecordStreamHandler) thread = threading.Thread(target=server.serve_forever, daemon=True) thread.start() while 1: time.sleep(1) logger.info("Message from server")# client.py import time from loguru import logger from logging.handlers import SocketHandler socket_handler = SocketHandler('localhost', 9999) logger.remove() logger.add(socket_handler) while 1: time.sleep(1) logger.info("Message from client")I tested it and it seems to work, but this is not as elegant as I would like. Ideally, you would just log the unpickled record, so that formatting depends on your server (and not client) handlers. Keep in mind that
pickle
is unsafe, so you maybe better should serialize the logs using JSON.
thanks a lot! happy Chinese new year!
from loguru.
Im not sure the above example will work from multiple clients. I think you would have to remove after each message sent.
from loguru.
@Mango-kid What do you mean? What should be removed?
from loguru.
If you want to have multiple log clients connected you will have to
logger.remove()
logger.debug("some log message")
logger.add(socket_handler)
for every logging event. I dont think the server in the above cookbook code will handle more that one connection because each logger leaves the connection open.
from loguru.
Related Issues (20)
- How to display taskName of async task in loguru? HOT 2
- Using custom logger across multiple modules HOT 2
- How split sinks by behaviour? HOT 1
- Can `loguru` conditionally display the extra field if the data is non-empty only? HOT 2
- message being shown twice HOT 2
- confuse about creating independent loggers with separate set of handlers HOT 1
- Support tree-like view for logs HOT 1
- Allow using `opt` method as a context manager HOT 1
- Supress bold while preserving colours HOT 3
- PermissionError: [WinError 32] The process cannot access the file because it is being used by another process HOT 6
- Issue with custom log levels and Rich handlers in loguru HOT 1
- Loguru writing exception logs in addition to custom (Rich) handler HOT 1
- Log have no color in docker logs HOT 1
- Allow for combining file rotation triggers by accepting a list of rotation conditions
- How can I retrieve loguru descriptive traceback? HOT 2
- Question: Why does pytest not recognise AssertionError when enqueue=True? HOT 3
- Support the implementation of https://no-color.org/
- How to Access Contextualized Values Within do_something() Function? HOT 2
- file logs change format HOT 1
- how to log ip addresses in log HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loguru.