Skip to content

Commit

Permalink
add cmd.log_server
Browse files Browse the repository at this point in the history
  • Loading branch information
lidong committed Aug 10, 2024
1 parent 7c7fb55 commit b8bd25b
Show file tree
Hide file tree
Showing 6 changed files with 457 additions and 190 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ print(morebuiltins.__file__)

[Changelog](https://github.com/ClericPy/morebuiltins/blob/master/CHANGELOG.md)

## More Modules:

---

Expand Down Expand Up @@ -184,4 +185,22 @@ print(morebuiltins.__file__)
8.1 `SimpleEmail` - SimpleEmail Sender.


## 9. morebuiltins.cmd.log_server

9.1 `LogServer` - Log Server for SocketHandler, create a socket server with asyncio.start_server. Update settings of rotation/formatter with extra: {"max_size": 1024**2, "formatter": logging.Formatter(fmt="%(asctime)s - %(filename)s - %(message)s")}


<!-- end -->

## cmd utils

1. download_python
1. `python -m morebuiltins.cmd.download_python -a -k 3.11 -u`
2. `-a` will filt with current platform(x86_64+install_only), `-k` is the keywords, `-u` will unzip the tar.gz
2. zipapps
1. `python -m morebuiltins.zipapps -h`
2. https://github.com/ClericPy/zipapps
3. log_server
1. `python -m morebuiltins.cmd.log_server --log-dir=./logs`
2. client use the `logging.handlers.SocketHandler` (support python2/3)
3. Update settings of rotation/formatter with `extra: {"max_size": 1024**2, "formatter": logging.Formatter(fmt="%(asctime)s - %(filename)s - %(message)s")}`
75 changes: 73 additions & 2 deletions doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -1408,14 +1408,17 @@
>>> writer = RotatingFileWriter("test.log", max_size=10 * 1024, max_backups=1)
>>> writer.write("1" * 10)
>>> writer.path.stat().st_size
0
>>> writer.flush()
>>> writer.path.stat().st_size
10
>>> writer.clean_backups(writer.max_backups)
>>> writer.unlink_file()
>>> # test rotating
>>> writer = RotatingFileWriter("test.log", max_size=20, max_backups=2)
>>> writer.write("1" * 15)
>>> writer.write("1" * 15)
>>> writer.write("1" * 15)
>>> writer.write("1" * 15, flush=True)
>>> writer.path.stat().st_size
15
>>> len(writer.backup_path_list())
Expand All @@ -1426,7 +1429,7 @@
>>> writer = RotatingFileWriter("test.log", max_size=20, max_backups=0)
>>> writer.write("1" * 15)
>>> writer.write("1" * 15)
>>> writer.write("1" * 15)
>>> writer.write("1" * 15, flush=True)
>>> writer.path.stat().st_size
15
>>> len(writer.backup_path_list())
Expand Down Expand Up @@ -1883,3 +1886,71 @@
---


## 9. morebuiltins.cmd.log_server



9.1 `LogServer` - Log Server for SocketHandler, create a socket server with asyncio.start_server. Update settings of rotation/formatter with extra: {"max_size": 1024**2, "formatter": logging.Formatter(fmt="%(asctime)s - %(filename)s - %(message)s")}


```python

Server side:
python -m morebuiltins.cmd.log_server --log-dir=./logs --host 127.0.0.1 --port 8901

Client side:

```python
import logging
import logging.handlers

logger = logging.getLogger("client")
logger.setLevel(logging.DEBUG)
h = logging.handlers.SocketHandler("127.0.0.1", 8901)
h.setLevel(logging.DEBUG)
logger.addHandler(h)
for _ in range(5):
logger.info(
"hello world!",
extra={
"max_size": 1024**2,
"formatter": logging.Formatter(
fmt="%(asctime)s - %(filename)s - %(message)s"
),
},
)
# [client] 2024-08-10 19:30:07,113 - temp3.py - hello world!
# [client] 2024-08-10 19:30:07,113 - temp3.py - hello world!
# [client] 2024-08-10 19:30:07,113 - temp3.py - hello world!
# [client] 2024-08-10 19:30:07,113 - temp3.py - hello world!
# [client] 2024-08-10 19:30:07,114 - temp3.py - hello world!
```

More docs:
python -m morebuiltins.cmd.log_server -h
usage: log_server.py [-h] [--host HOST] [--port PORT] [--log-dir LOG_DIR] [--name NAME] [--server-log-args SERVER_LOG_ARGS] [--handle-signals HANDLE_SIGNALS] [--max-queue-size MAX_QUEUE_SIZE]
[--max-queue-buffer MAX_QUEUE_BUFFER] [--log-stream LOG_STREAM]

options:
-h, --help show this help message and exit
--host HOST
--port PORT
--log-dir LOG_DIR log dir to save log files, if empty, log to stdout with --log-stream
--name NAME log server name
--server-log-args SERVER_LOG_ARGS
max_size,max_backups for log files, default: 10485760,5 == 10MB each log file, 1 name.log + 5 backups
--handle-signals HANDLE_SIGNALS
--max-queue-size MAX_QUEUE_SIZE
max queue size for log queue, log will be in memory queue before write to file
--max-queue-buffer MAX_QUEUE_BUFFER
chunk size of lines before write to file
--log-stream LOG_STREAM
log to stream, if --log-stream='' will mute the stream log


```


---


3 changes: 2 additions & 1 deletion morebuiltins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.1.0"
__version__ = "1.1.1"
__all__ = [
"morebuiltins.utils",
"morebuiltins.date",
Expand All @@ -8,4 +8,5 @@
"morebuiltins.download_python",
"morebuiltins.tk",
"morebuiltins.emails",
"morebuiltins.cmd.log_server",
]
Loading

0 comments on commit b8bd25b

Please sign in to comment.