Skip to content

Commit 1dc43a0

Browse files
authored
Merge pull request #22 from bybatkhuu/develop
🎨 Merged config schemas into one.
2 parents 541e4b4 + d463789 commit 1dc43a0

File tree

5 files changed

+101
-196
lines changed

5 files changed

+101
-196
lines changed

README.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ It is a `Loguru` based custom logging package for python projects.
2424
- Add custom **handlers**
2525
- **FastAPI** HTTP access logging **middleware**
2626
- **Base** logging module
27+
- Support **Pydantic-v1** and **Pydantic-v2**
2728

2829
---
2930

@@ -181,16 +182,16 @@ python ./main.py
181182
**Output**:
182183

183184
```txt
184-
[2023-09-01 09:00:00.384 +09:00 | TRACE | beans_logging._base:478]: Intercepted modules: ['concurrent', 'concurrent.futures', 'asyncio']; Muted modules: [];
185-
[2023-09-01 09:00:00.384 +09:00 | TRACE | __main__:7]: Tracing...
186-
[2023-09-01 09:00:00.385 +09:00 | DEBUG | __main__:8]: Debugging...
187-
[2023-09-01 09:00:00.385 +09:00 | INFO | __main__:9]: Logging info.
188-
[2023-09-01 09:00:00.385 +09:00 | OK | __main__:10]: Success.
189-
[2023-09-01 09:00:00.385 +09:00 | WARN | __main__:11]: Warning something.
190-
[2023-09-01 09:00:00.385 +09:00 | ERROR | __main__:12]: Error occured.
191-
[2023-09-01 09:00:00.386 +09:00 | CRIT | __main__:13]: CRITICAL ERROR.
192-
[2023-09-01 09:00:00.386 +09:00 | ERROR | __main__:25]: division by zero
193-
[2023-09-01 09:00:00.386 +09:00 | ERROR | __main__:32]: Show me, what value is wrong:
185+
[2023-09-01 00:00:00.000 +09:00 | TRACE | beans_logging._base:478]: Intercepted modules: ['concurrent', 'concurrent.futures', 'asyncio']; Muted modules: [];
186+
[2023-09-01 00:00:00.000 +09:00 | TRACE | __main__:7]: Tracing...
187+
[2023-09-01 00:00:00.000 +09:00 | DEBUG | __main__:8]: Debugging...
188+
[2023-09-01 00:00:00.000 +09:00 | INFO | __main__:9]: Logging info.
189+
[2023-09-01 00:00:00.000 +09:00 | OK | __main__:10]: Success.
190+
[2023-09-01 00:00:00.000 +09:00 | WARN | __main__:11]: Warning something.
191+
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:12]: Error occured.
192+
[2023-09-01 00:00:00.000 +09:00 | CRIT | __main__:13]: CRITICAL ERROR.
193+
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:25]: division by zero
194+
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:32]: Show me, what value is wrong:
194195
Traceback (most recent call last):
195196
196197
> File "/home/user/workspaces/projects/beans_logging/examples/simple/./main.py", line 30, in <module>
@@ -344,22 +345,22 @@ uvicorn main:app --host=0.0.0.0 --port=8000
344345
**Output**:
345346

346347
```txt
347-
[2023-09-01 14:55:11.724 +09:00 | TRACE | beans_logging._base:576]: Intercepted modules: ['watchfiles.watcher', 'dotenv', 'asyncio', 'dotenv.main', 'watchfiles.main', 'concurrent.futures', 'uvicorn', 'fastapi', 'concurrent', 'watchfiles']; Muted modules: ['uvicorn.access', 'uvicorn.error'];
348-
[2023-09-01 14:55:11.740 +09:00 | INFO | uvicorn.server:76]: Started server process [17146]
349-
[2023-09-01 14:55:11.740 +09:00 | INFO | uvicorn.lifespan.on:46]: Waiting for application startup.
350-
[2023-09-01 14:55:11.741 +09:00 | INFO | main:21]: Preparing to startup...
351-
[2023-09-01 14:55:11.741 +09:00 | OK | main:22]: Finished preparation to startup.
352-
[2023-09-01 14:55:11.741 +09:00 | INFO | main:23]: API version: 0.0.1-000000
353-
[2023-09-01 14:55:11.741 +09:00 | INFO | uvicorn.lifespan.on:60]: Application startup complete.
354-
[2023-09-01 14:55:11.745 +09:00 | INFO | uvicorn.server:218]: Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
355-
[2023-09-01 14:55:17.417 +09:00 | DEBUG | anyio._backends._asyncio:833]: [f635ebbc3f2348db9dcff681be1bd52a] 127.0.0.1 - "GET / HTTP/1.1"
356-
[2023-09-01 14:55:17.418 +09:00 | OK | anyio._backends._asyncio:833]: [f635ebbc3f2348db9dcff681be1bd52a] 127.0.0.1 - "GET / HTTP/1.1" 200 17B 0.7ms
357-
^C[2023-09-01 14:55:18.729 +09:00 | INFO | uvicorn.server:264]: Shutting down
358-
[2023-09-01 14:55:18.831 +09:00 | INFO | uvicorn.lifespan.on:65]: Waiting for application shutdown.
359-
[2023-09-01 14:55:18.834 +09:00 | INFO | main:26]: Praparing to shutdown...
360-
[2023-09-01 14:55:18.835 +09:00 | OK | main:27]: Finished preparation to shutdown.
361-
[2023-09-01 14:55:18.837 +09:00 | INFO | uvicorn.lifespan.on:76]: Application shutdown complete.
362-
[2023-09-01 14:55:18.837 +09:00 | INFO | uvicorn.server:86]: Finished server process [17146]
348+
[2023-09-01 00:00:00.000 +09:00 | TRACE | beans_logging._base:576]: Intercepted modules: ['watchfiles.watcher', 'dotenv', 'asyncio', 'dotenv.main', 'watchfiles.main', 'concurrent.futures', 'uvicorn', 'fastapi', 'concurrent', 'watchfiles']; Muted modules: ['uvicorn.access', 'uvicorn.error'];
349+
[2023-09-01 00:00:00.000 +09:00 | INFO | uvicorn.server:76]: Started server process [17146]
350+
[2023-09-01 00:00:00.000 +09:00 | INFO | uvicorn.lifespan.on:46]: Waiting for application startup.
351+
[2023-09-01 00:00:00.000 +09:00 | INFO | main:21]: Preparing to startup...
352+
[2023-09-01 00:00:00.000 +09:00 | OK | main:22]: Finished preparation to startup.
353+
[2023-09-01 00:00:00.000 +09:00 | INFO | main:23]: API version: 0.0.1-000000
354+
[2023-09-01 00:00:00.000 +09:00 | INFO | uvicorn.lifespan.on:60]: Application startup complete.
355+
[2023-09-01 00:00:00.000 +09:00 | INFO | uvicorn.server:218]: Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
356+
[2023-09-01 00:00:00.000 +09:00 | DEBUG | anyio._backends._asyncio:833]: [f635ebbc3f2348db9dcff681be1bd52a] 127.0.0.1 - "GET / HTTP/1.1"
357+
[2023-09-01 00:00:00.000 +09:00 | OK | anyio._backends._asyncio:833]: [f635ebbc3f2348db9dcff681be1bd52a] 127.0.0.1 - "GET / HTTP/1.1" 200 17B 0.7ms
358+
^C[2023-09-01 00:00:00.000 +09:00 | INFO | uvicorn.server:264]: Shutting down
359+
[2023-09-01 00:00:00.000 +09:00 | INFO | uvicorn.lifespan.on:65]: Waiting for application shutdown.
360+
[2023-09-01 00:00:00.000 +09:00 | INFO | main:26]: Praparing to shutdown...
361+
[2023-09-01 00:00:00.000 +09:00 | OK | main:27]: Finished preparation to shutdown.
362+
[2023-09-01 00:00:00.000 +09:00 | INFO | uvicorn.lifespan.on:76]: Application shutdown complete.
363+
[2023-09-01 00:00:00.000 +09:00 | INFO | uvicorn.server:86]: Finished server process [17146]
363364
```
364365

365366
---

beans_logging/schemas/config.py renamed to beans_logging/schemas.py

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,27 @@
33
import datetime
44
from typing import List
55

6-
from pydantic import (
7-
BaseModel,
8-
constr,
9-
Field,
10-
field_validator,
11-
model_validator,
12-
ConfigDict,
13-
)
146

15-
from .._consts import LogLevelEnum
16-
from .._utils import get_default_logs_dir, get_app_name
7+
import pydantic
8+
from pydantic import BaseModel, constr, Field
9+
10+
if "2.0.0" <= pydantic.__version__:
11+
from pydantic import field_validator, model_validator, ConfigDict
12+
else:
13+
from pydantic import validator, root_validator
14+
15+
16+
from ._consts import LogLevelEnum
17+
from ._utils import get_default_logs_dir, get_app_name
1718

1819

1920
class ExtraBaseModel(BaseModel):
20-
model_config = ConfigDict(extra="allow")
21+
if "2.0.0" <= pydantic.__version__:
22+
model_config = ConfigDict(extra="allow")
23+
else:
24+
25+
class Config:
26+
extra = "allow"
2127

2228

2329
class StdHandlerPM(ExtraBaseModel):
@@ -49,14 +55,26 @@ class LogHandlersPM(ExtraBaseModel):
4955
default="{app_name}.std.err.log", min_length=4, max_length=1023
5056
)
5157

52-
@model_validator(mode="after")
53-
def _check_log_path(self) -> "LogHandlersPM":
54-
if self.log_path == self.err_path:
55-
raise ValueError(
56-
f"`log_path` and `err_path` attributes are same: '{self.log_path}', must be different!"
57-
)
58+
if "2.0.0" <= pydantic.__version__:
59+
60+
@model_validator(mode="after")
61+
def _check_log_path(self) -> "LogHandlersPM":
62+
if self.log_path == self.err_path:
63+
raise ValueError(
64+
f"`log_path` and `err_path` attributes are same: '{self.log_path}', must be different!"
65+
)
66+
return self
67+
68+
else:
5869

59-
return self
70+
@root_validator
71+
def _check_log_path(cls, values):
72+
_log_path, _err_path = values.get("log_path"), values.get("err_path")
73+
if _log_path == _err_path:
74+
raise ValueError(
75+
f"`log_path` and `err_path` attributes are same: '{_log_path}', must be different!"
76+
)
77+
return values
6078

6179

6280
class JsonHandlersPM(ExtraBaseModel):
@@ -69,14 +87,26 @@ class JsonHandlersPM(ExtraBaseModel):
6987
default="{app_name}.json.err.log", min_length=4, max_length=1023
7088
)
7189

72-
@model_validator(mode="after")
73-
def _check_log_path(self) -> "JsonHandlersPM":
74-
if self.log_path == self.err_path:
75-
raise ValueError(
76-
f"`log_path` and `err_path` attributes are same: '{self.log_path}', must be different!"
77-
)
90+
if "2.0.0" <= pydantic.__version__:
7891

79-
return self
92+
@model_validator(mode="after")
93+
def _check_log_path(self) -> "JsonHandlersPM":
94+
if self.log_path == self.err_path:
95+
raise ValueError(
96+
f"`log_path` and `err_path` attributes are same: '{self.log_path}', must be different!"
97+
)
98+
return self
99+
100+
else:
101+
102+
@root_validator
103+
def _check_log_path(cls, values):
104+
_log_path, _err_path = values.get("log_path"), values.get("err_path")
105+
if _log_path == _err_path:
106+
raise ValueError(
107+
f"`log_path` and `err_path` attributes are same: '{_log_path}', must be different!"
108+
)
109+
return values
80110

81111

82112
class FilePM(ExtraBaseModel):
@@ -94,12 +124,22 @@ class FilePM(ExtraBaseModel):
94124
log_handlers: LogHandlersPM = Field(default_factory=LogHandlersPM)
95125
json_handlers: JsonHandlersPM = Field(default_factory=JsonHandlersPM)
96126

97-
@field_validator("rotate_time", mode="before")
98-
@classmethod
99-
def _check_rotate_time(cls, val):
100-
if isinstance(val, str):
101-
val = datetime.time.fromisoformat(val)
102-
return val
127+
if "2.0.0" <= pydantic.__version__:
128+
129+
@field_validator("rotate_time", mode="before")
130+
@classmethod
131+
def _check_rotate_time(cls, val):
132+
if isinstance(val, str):
133+
val = datetime.time.fromisoformat(val)
134+
return val
135+
136+
else:
137+
138+
@validator("rotate_time", pre=True, always=True)
139+
def _check_rotate_time(cls, val):
140+
if val and isinstance(val, str):
141+
val = datetime.time.fromisoformat(val)
142+
return val
103143

104144

105145
class AutoLoadPM(ExtraBaseModel):

beans_logging/schemas/__init__.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

beans_logging/schemas/config_v1.py

Lines changed: 0 additions & 128 deletions
This file was deleted.

scripts/build.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fi
2727
## --- Variables --- ##
2828
# Flags:
2929
_IS_CLEAN=true
30-
_IS_TEST=true
30+
_IS_TEST=false
3131
_IS_UPLOAD=false
3232
_IS_STAGING=true
3333
## --- Variables --- ##
@@ -44,7 +44,7 @@ main()
4444
_IS_CLEAN=false
4545
shift;;
4646
-t | --disable-test)
47-
_IS_TEST=false
47+
_IS_TEST=true
4848
shift;;
4949
-u | --upload)
5050
_IS_UPLOAD=true
@@ -54,7 +54,7 @@ main()
5454
shift;;
5555
*)
5656
echoError "Failed to parsing input -> ${_input}"
57-
echoInfo "USAGE: ${0} -c, --disable-clean | -t, --disable-test | -u, --upload | -p, --production"
57+
echoInfo "USAGE: ${0} -c, --disable-clean | -t, --test | -u, --upload | -p, --production"
5858
exit 1;;
5959
esac
6060
done

0 commit comments

Comments
 (0)