Skip to content

Commit 95e6856

Browse files
committed
♻️ Default None for auto-flush, default directory '.', update README
1 parent 5317123 commit 95e6856

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,26 @@ You can use the python package manager (`pip`) to install the file-logger:
1313
pip install jsonfilelogger
1414
```
1515

16-
## Usage
16+
## Usage: LogWriter
1717

1818
Create a `LogWriter`:
1919

2020
```python
2121
from jsonfilelogger.logger import LogWriter
2222

23-
writer = LogWriter(folder="./", filename="log.json", threshold=10)
23+
writer = LogWriter(folder=".", filename="log.json", threshold=10)
2424
```
2525

26+
### Parameters
27+
28+
`folder`: the folder where the logfile should be stored
29+
30+
`filename`: the name of the logfile
31+
32+
`threshold`: the threshold for auto-flushing; if set None the writer will not autoflush
33+
34+
### Example
35+
2636
Log data to the logfile:
2737

2838
```python
@@ -34,6 +44,8 @@ writer.log({"key_int": 1,
3444
"key_none": None})
3545
```
3646

47+
## Usage: LogReader
48+
3749
Create a `LogReader`:
3850

3951
```python
@@ -42,13 +54,22 @@ from jsonfilelogger.logger import LogReader
4254
reader = LogReader(folder="./", filename="log.json")
4355
```
4456

57+
### Parameters
58+
59+
`folder`: the folder where the logfile should be stored
60+
61+
`filename`: the name of the logfile
62+
63+
### Example
64+
4565
Read data from the logfile:
4666

4767
```python
4868
data = reader.retrieve()
69+
... # do something with the data
4970
```
5071

51-
Other methods:
72+
## Methods
5273

5374
| Class | Method | Explanation |
5475
|:-----|:--------|:------|
@@ -59,7 +80,7 @@ Other methods:
5980
| LogWriter | `.flush()` | This method flushes the buffer in the writer. The threshold of the buffer is given in the constructor |
6081
| LogReader | `.retrieve()` | This method retrieves all the data from the logfile and returns it as a list of dictionaries. |
6182

62-
## Example usage
83+
## Demo usage
6384

6485
An example of how to use this logger is given. Imagine one has created a [major breakthrough AI system](https://i.pinimg.com/originals/ae/fb/01/aefb01c27ddfdfa2cef723f5056252f7.jpg) that still has to be trained.
6586
During training one want's to keep an eye on the performance of the progress. To do this, the LogWriter can be added in the learning iterations (e.g. in one Jupyter notebook).

jsonfilelogger/logger.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class LogWriter(LogBase):
5050
LogWriter for writing contents to the logfiles.
5151
"""
5252

53-
def __init__(self, folder: str = "./", filename: str = "log.json", threshold: int = 16):
53+
def __init__(self, folder: str = "./", filename: str = "log.json", threshold: int or None = None):
5454
LogBase.__init__(self, folder, filename)
5555
self.log_buffer = [] # buffer of all logs before being written to file
5656
self.threshold = threshold # threshold before writing to file
@@ -74,7 +74,7 @@ def log(self, data):
7474
:return: None
7575
"""
7676
self.log_buffer.append(data)
77-
if len(self.log_buffer) >= self.threshold:
77+
if self.threshold is not None and len(self.log_buffer) >= self.threshold:
7878
# write all data to file and flush buffer
7979
self.flush()
8080

@@ -126,7 +126,7 @@ class LogReader(LogBase):
126126
LogReader for reading contents from the logfiles.
127127
"""
128128

129-
def __init__(self, folder: str = "./", filename: str = "log.json"):
129+
def __init__(self, folder: str = ".", filename: str = "log.json"):
130130
LogBase.__init__(self, folder, filename)
131131
self.assert_file_exists()
132132

0 commit comments

Comments
 (0)