Skip to content

Commit 6f0b097

Browse files
committed
wip: uvicorn logging conf to json
1 parent a58e98c commit 6f0b097

File tree

9 files changed

+165
-56
lines changed

9 files changed

+165
-56
lines changed

app/api/stuff.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ async def create_multi_stuff(
2121
db_session.add_all(stuff_instances)
2222
await db_session.commit()
2323
except SQLAlchemyError as ex:
24+
logger.error(f"Error inserting instances of Stuff: {repr(ex)}")
2425
raise HTTPException(
2526
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
2627
) from ex

app/models/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
66
from sqlalchemy.ext.asyncio import AsyncSession
77
from sqlalchemy.orm import declared_attr, DeclarativeBase
8+
from app.utils.logging import AppLogger
9+
10+
logger = AppLogger().get_logger()
811

912

1013
class Base(DeclarativeBase):
@@ -26,6 +29,7 @@ async def save(self, db_session: AsyncSession):
2629
db_session.add(self)
2730
return await db_session.commit()
2831
except SQLAlchemyError as ex:
32+
logger.error(f"Error inserting instance of {self}: {repr(ex)}")
2933
raise HTTPException(
3034
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
3135
) from ex

app/utils/logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ def get_logger(self):
2020
class RichConsoleHandler(RichHandler):
2121
def __init__(self, width=200, style=None, **kwargs):
2222
super().__init__(
23-
console=Console(color_system="256", width=width, style=style), **kwargs
23+
console=Console(color_system="256", width=width, style=style, stderr=True), **kwargs
2424
)

compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ services:
77
- .secrets
88
command: bash -c "
99
uvicorn app.main:app
10-
--log-config ./config.ini
10+
--log-config ./uvicorn-logging.json
1111
--host 0.0.0.0 --port 8080
1212
--lifespan=on --use-colors --loop uvloop --http httptools
1313
--reload --log-level debug

config.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ qualname = uvicorn.access
2727
[handler_stream]
2828
class = app.utils.logging.RichConsoleHandler
2929
kwargs = {"omit_repeated_times":True, "show_time": False, "enable_link_path": False, "tracebacks_show_locals": True, "rich_tracebacks": True}
30-
args = (100, "white")
30+
args = (140, "white")
3131
formatter = default
3232
stream = ext://sys.stdout
3333

3434
[handler_sqlalchemy]
3535
class = app.utils.logging.RichConsoleHandler
3636
kwargs = {"omit_repeated_times":True, "show_time": False, "enable_link_path": False, "tracebacks_show_locals": True, "rich_tracebacks": True}
37-
args = (100, "magenta")
37+
args = (140, "magenta")
3838
formatter = default
3939

4040
[handler_uvicorn]
4141
class = app.utils.logging.RichConsoleHandler
4242
kwargs = {"omit_repeated_times":True, "show_time": False, "enable_link_path": False, "tracebacks_show_locals": True, "rich_tracebacks": True}
43-
args = (100, "yellow")
43+
args = (140, "yellow")
4444
formatter = default
4545

4646
[formatter_default]

granian-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services:
55
env_file:
66
- .env
77
- .secrets
8-
command: granian --interface asgi --host 0.0.0.0 --port 8080 --loop uvloop app.main:app --log-level debug
8+
command: granian --interface asgi --host 0.0.0.0 --port 8080 --loop uvloop app.main:app --log-level debug --log-config ./logging-config.json
99
volumes:
1010
- .:/home/code
1111
ports:

logging-config.json

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
11
{
2-
"version": 1,
3-
"disable_existing_loggers": false,
4-
"formatters": {
5-
"default": {
6-
"format": "[%(process)d|%(name)-12s] %(message)s",
7-
"class": "logging.Formatter"
8-
}
2+
"version": 1,
3+
"disable_existing_loggers": false,
4+
"formatters": {
5+
"generic": {
6+
"()": "logging.Formatter",
7+
"fmt": "[%(process)d|%(name)-12s] %(message)s",
8+
"datefmt": "[%Y-%m-%d %H:%M:%S %z]"
99
},
10-
"handlers": {
11-
"console": {
12-
"class": "app.utils.logging.RichConsoleHandler",
13-
"level": "DEBUG",
14-
"formatter": "default",
15-
"stream": "ext://sys.stdout",
16-
"kwargs": {"omit_repeated_times": "True", "show_time": "False", "enable_link_path": "False", "tracebacks_show_locals": "True", "rich_tracebacks": "True"},
17-
"args": "(100, 'white')"
18-
19-
},
20-
"sqlalchemy": {
21-
// "class": "logging.handlers.TimedRotatingFileHandler",
22-
// "level": "DEBUG",
23-
// "formatter": "verbose",
24-
// "when": "D",
25-
// "backupCount": 0,
26-
// "filename": "./logs/training-stats.log"
27-
},
28-
"granian": {
29-
// "class": "logging.handlers.TimedRotatingFileHandler",
30-
// "level": "DEBUG",
31-
// "formatter": "verbose",
32-
// "when": "D",
33-
// "backupCount": 0,
34-
// "filename": "./logs/uvicorn.log"
35-
}
10+
"access": {
11+
"()": "logging.Formatter",
12+
"fmt": "[%(process)d|%(name)-12s] %(message)s",
13+
"datefmt": "[%Y-%m-%d %H:%M:%S %z]"
14+
}
15+
},
16+
"handlers": {
17+
"console": {
18+
"formatter": "generic",
19+
"class": "logging.StreamHandler",
20+
"stream": "ext://sys.stdout"
21+
},
22+
"access": {
23+
"formatter": "access",
24+
"class": "logging.StreamHandler",
25+
"stream": "ext://sys.stdout"
26+
},
27+
"sqlalchemy": {
28+
"class": "app.utils.logging.RichConsoleHandler",
29+
"formatter": "generic"
30+
}
31+
},
32+
"loggers": {
33+
"_granian": {
34+
"handlers": [
35+
"console"
36+
],
37+
"level": "INFO",
38+
"propagate": false
39+
},
40+
"granian.access": {
41+
"handlers": [
42+
"access"
43+
],
44+
"level": "INFO",
45+
"propagate": false
3646
},
37-
"loggers": {
38-
"root": {
39-
// "level": "DEBUG",
40-
// "handlers": ["console"]
41-
},
42-
"sqlalchemy.engine.Engine": {
43-
// "level": "DEBUG",
44-
// "handlers": ["file"],
45-
// "propagate": true,
46-
// "qualname": "app"
47-
},
48-
"granian.access": {
49-
// "level": "DEBUG",
50-
// "handlers": ["uvicorn"],
51-
// "propagate": true,
52-
// "qualname": "uvicorn"
53-
}
47+
"sqlalchemy.engine.Engine": {
48+
"handlers": [
49+
"sqlalchemy"
50+
],
51+
"level": "ERROR",
52+
"propagate": true,
53+
"qualname": "sqlalchemy.engine.Engine"
5454
}
55+
}
5556
}

uvicorn-logging-old.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
3+
{
4+
"version": 1,
5+
"disable_existing_loggers": false,
6+
"formatters": {
7+
"default": {
8+
"()": "logging.Formatter",
9+
"fmt": "[%(process)d|%(name)-12s] %(message)s"
10+
}
11+
},
12+
"handlers": {
13+
"stream": {
14+
"formatter": "default",
15+
"class": "app.utils.logging.RichConsoleHandler",
16+
"stream": "ext://sys.stdout"
17+
},
18+
"uvicorn": {
19+
"formatter": "default",
20+
"class": "app.utils.logging.RichConsoleHandler",
21+
"stream": "ext://sys.stdout"
22+
},
23+
"sqlalchemy": {
24+
"formatter": "default",
25+
"class": "app.utils.logging.RichConsoleHandler"
26+
}
27+
},
28+
"loggers": {
29+
"root": {
30+
"handlers": [
31+
"stream"
32+
],
33+
"level": "INFO",
34+
"propagate": false
35+
},
36+
"uvicorn.access": {
37+
"handlers": [
38+
"uvicorn"
39+
],
40+
"level": "INFO",
41+
"propagate": false
42+
},
43+
"sqlalchemy.engine.Engine": {
44+
"handlers": [
45+
"sqlalchemy"
46+
],
47+
"level": "ERROR",
48+
"propagate": true,
49+
"qualname": "sqlalchemy.engine.Engine"
50+
}
51+
}
52+
}

uvicorn-logging.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"version": 1,
3+
"disable_existing_loggers": false,
4+
"formatters": {
5+
"default": {
6+
"()": "logging.Formatter",
7+
"fmt": "[%(process)d|%(name)-12s] %(message)s"
8+
}
9+
},
10+
"handlers": {
11+
"uvicorn": {
12+
"class": "app.utils.logging.RichConsoleHandler",
13+
"omit_repeated_times": true,
14+
"show_time": false,
15+
"enable_link_path": false,
16+
"tracebacks_show_locals": true,
17+
"rich_tracebacks": true,
18+
"formatter": "default",
19+
"width": 140,
20+
"style": "yellow"
21+
},
22+
"sqlalchemy": {
23+
"class": "app.utils.logging.RichConsoleHandler",
24+
"omit_repeated_times": true,
25+
"show_time": false,
26+
"enable_link_path": false,
27+
"tracebacks_show_locals": true,
28+
"rich_tracebacks": true,
29+
"formatter": "default",
30+
"width": 140,
31+
"style": "magenta"
32+
}
33+
},
34+
"loggers": {
35+
"root": {
36+
"handlers": [
37+
"uvicorn"
38+
],
39+
"propagate": false,
40+
"level": "TRACE"
41+
},
42+
"sqlalchemy.engine.Engine": {
43+
"handlers": [
44+
"sqlalchemy"
45+
],
46+
"level": "ERROR",
47+
"propagate": false,
48+
"qualname": "sqlalchemy.engine.Engine"
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)