Skip to content

Commit

Permalink
async logger fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
aldo authored and aldo committed Jun 21, 2024
1 parent 8a5e9f4 commit 2731161
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/instructlab/training/async_logger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# File: async_logger.py

import json
import asyncio
from datetime import datetime
Expand All @@ -17,7 +15,7 @@ def __init__(self, file_name='training_log.jsonl'):
asyncio.run_coroutine_threadsafe(self._initialize_log_file(), self.loop)

def _run_event_loop(self, loop):
asyncio.set_event_loop(loop)
asyncio.set_event_loop(loop) #
loop.run_forever()

async def _initialize_log_file(self):
Expand All @@ -28,28 +26,26 @@ async def _initialize_log_file(self):
if line.strip(): # Avoid empty lines
self.logs.append(json.loads(line.strip()))
except FileNotFoundError:
# File does not exist but the first log will create it.
pass


async def log(self, data):
'''logs a dictionary as a new line in a jsonl file with a timestamp'''
if not isinstance(data, dict):
raise ValueError("Logged data must be a dictionary")
data['timestamp'] = datetime.now().isoformat()
self.logs.append(data)
await self._write_logs_to_file()

async def _write_logs_to_file(self):
temp_file_name = f"{self.file_name}.tmp"
async with aiofiles.open(temp_file_name, 'w') as temp_file:
await temp_file.write(json.dumps(self.logs[-1], indent=None) + '\n')
await temp_file.flush() # Flush the file buffer
os.fsync(temp_file.fileno()) # Sync the file with the storage device
await self._write_logs_to_file(data)

# Rename the temporary file to the main file name
os.replace(temp_file_name, self.file_name)
async def _write_logs_to_file(self, data):
'''appends to the log instead of writing the whole log each time'''
async with aiofiles.open(self.file_name, 'a') as f:
await f.write(json.dumps(data, indent=None) + '\n')

def log_sync(self, data: dict):
'''runs the log coroutine non-blocking'''
asyncio.run_coroutine_threadsafe(self.log(data), self.loop)

def __repr__(self):
return f"<AsyncStructuredLogger(file_name={self.file_name})>"
return f"<AsyncStructuredLogger(file_name={self.file_name})>"

0 comments on commit 2731161

Please sign in to comment.