Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
elp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
崔为之
elp
Commits
fe0e6f82
Commit
fe0e6f82
authored
Nov 04, 2023
by
崔为之
💪🏽
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update project
parent
41394999
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
5 deletions
+41
-5
application/config/config.yaml
application/config/config.yaml
+3
-0
application/libs/flask_loguru/logger.py
application/libs/flask_loguru/logger.py
+38
-5
No files found.
application/config/config.yaml
View file @
fe0e6f82
...
...
@@ -20,3 +20,6 @@ Elasticsearch:
CaCerts
:
Index
:
filebeat-ctocst_router-2021.11
Type
:
_doc
Logger
:
Level
:
DEBUG
application/libs/flask_loguru/logger.py
View file @
fe0e6f82
...
...
@@ -10,35 +10,60 @@
# @Description :
"""
import
os
import
sys
from
flask
import
Flask
,
request
,
g
from
loguru
import
logger
from
.format
import
patching
from
application.common
import
ConfigHelper
class
FlaskLoguru
:
def
__init__
(
self
,
app
=
None
):
"""
Initialize the FlaskLoguru instance.
If an app is provided, also initialize that app for logging.
"""
if
app
is
not
None
:
self
.
init_app
(
app
)
def
init_app
(
self
,
app
:
Flask
)
->
None
:
"""Initialize the app"""
"""
Initialize the given app for logging with Loguru.
This involves setting up a new logger and registering it with the app.
"""
# Remove any existing handlers from the logger
logger
.
remove
()
# 获取日志级别
level
=
app
.
config
.
Logger
.
Level
if
app
.
config
.
get
(
'Logger'
)
is
not
None
else
'DEBUG'
config_helper
=
ConfigHelper
(
app
)
# Fetch the logging configuration from the app's config
cfg
=
config_helper
.
Logger
# Get the log level from the config, or default to 'DEBUG' if not provided
level
=
cfg
.
Level
or
'DEBUG'
# Add a new handler to the logger with the configured log level
logger
.
add
(
sys
.
stderr
,
level
=
level
,
format
=
patching
)
# 注册扩展
# Register this logger with the app
# If the app does not already have an 'extensions' attribute, add one
if
not
hasattr
(
app
,
'extensions'
):
app
.
extensions
=
{}
# Initialize the app's 'loguru' extension if it does not already exist
app
.
extensions
.
setdefault
(
'loguru'
,
{})
# Register this logger with the app's 'loguru' extension
app
.
extensions
[
'loguru'
][
self
]
=
logger
@
app
.
before_request
def
before_request
():
"""
A function to run before each request.
Logs the start of the request.
"""
data
=
dict
(
url
=
request
.
url
,
method
=
request
.
method
,
...
...
@@ -50,11 +75,19 @@ class FlaskLoguru:
@
app
.
after_request
def
after_request
(
response
):
"""
A function to run after each request.
Logs the completion of the request.
"""
g
.
logger
.
info
(
'Request completed'
)
return
response
@
app
.
teardown_request
def
teardown_request
(
exception
=
None
):
"""
A function to run when ending each request,
either because the request was completed or because an error occurred.
"""
if
exception
:
data
=
dict
(
exception
=
str
(
exception
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment