I think you can combine the InterceptHandler() in the Loguru's Readme with the Django's LOGGING settings variable.
class InterceptHandler(logging.Handler):
def emit(self, record):
# Get corresponding Loguru level if it exists
try:
level = logger.level(record.levelname).name
except ValueError:
level = record.levelno
# Find caller from where originated the logged message
frame, depth = logging.currentframe(), 2
while frame.f_code.co_filename == logging.__file__:
frame = frame.f_back
depth += 1
logger.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage())
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'intercept': {
'()': InterceptHandler,
'level': 0,
},
},
'loggers': {
'django': {
'handlers': ['intercept'],
'level': None,
'propagate': True,
},
}
}
logger.remove()
logger.add(sys.stderr, format="[{time}] <lvl>{message}</>", level="INFO")
Does it generate the output you expected?
This looks like a good starting point although it is not having all variables as the example. Thanks @Delgan
although it is not having all variables as the example
Which variables and example are you referring too?
If you think this example can be improved, let me know. It was indeed a basic example.
the config from the example results in

the missing things are logger name(module that sent the log), stack trace formatter and coloring.
Surely I'm missing something. The snippet I provided generate the following logs on my computer:
August 14, 2020 - 06:43:12
Django version 3.1, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[2020-08-14T06:43:18.293381+0000] "GET / HTTP/1.1" 200 16351
[2020-08-14T06:43:18.433020+0000] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[2020-08-14T06:43:18.476148+0000] Not Found: /favicon.ico
[2020-08-14T06:43:18.476921+0000] "GET /favicon.ico HTTP/1.1" 404 1972
[2020-08-14T06:43:18.504801+0000] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
[2020-08-14T06:43:18.510395+0000] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[2020-08-14T06:43:18.527441+0000] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
[2020-08-14T06:44:09.146986+0000] "GET / HTTP/1.1" 200 16351
[2020-08-14T06:44:09.274918+0000] "GET /static/admin/css/fonts.css HTTP/1.1" 304 0
It looks the output you're seeing does not come from loguru configuration. :confused:
@Delgan the one in image is from the structuredlog config that I mentioned in the description's link. I was telling that those parts like module name, log level are missing from the loguru example you have mentioned.
Thanks for the clarification. :)
Indeed, the format I provided as an example was very basic and missed useful data. I guess you've figured out how to customize it by now. Otherwise, feel free to ask me for more help.
Thanks @Delgan .
Just one thing, is there any method to list all format items available for logger ?

like {time}, {message}
how we add new such item?
All values from the record dict can be used in the format (because messages are basically logged by calling format.format_map(record)). You can find the list here in the documentation: The record dict.
You can add your own by using logger.patch() if needed.