diff options
author | John Turner <jturner.usa@gmail.com> | 2025-09-06 23:37:38 -0400 |
---|---|---|
committer | John Turner <jturner.usa@gmail.com> | 2025-09-06 23:37:38 -0400 |
commit | 368e8d9faa92284249d7905ab9e2a53b27a4c8ca (patch) | |
tree | 632fcedb6f2ba6eec04c273509a01a3a2facf412 | |
parent | 756a4bee4ad7c83a55e67c63614d8424094ddf06 (diff) | |
download | pypaste-368e8d9faa92284249d7905ab9e2a53b27a4c8ca.tar.gz |
add logging
-rw-r--r-- | pypaste/__main__.py | 77 |
1 files changed, 57 insertions, 20 deletions
diff --git a/pypaste/__main__.py b/pypaste/__main__.py index 98de65f..0ac25fd 100644 --- a/pypaste/__main__.py +++ b/pypaste/__main__.py @@ -34,6 +34,26 @@ from pygments.lexers import guess_lexer, get_lexer_by_name from pygments.formatters import HtmlFormatter from pygments.styles import get_style_by_name +RESET = "\x1b[0m" +RED = "\x1b[31m" +GREEN = "\x1b[32m" +YELLOW = "\x1b[33m" + + +def log_info(msg: str): + now = datetime.now().isoformat() + print(f"{GREEN}[info]{RESET} {now} {msg}", file=sys.stderr) + + +def log_warning(msg: str): + now = datetime.now().isoformat() + print(f"{YELLOW}[warning]{RESET} {now} {msg}", file=sys.stderr) + + +def log_error(msg: str): + now = datetime.now().isoformat() + print(f"{RED}[warning]{RESET} {now} {msg}", file=sys.stderr) + def pygmentize( content: str, syntax: Optional[str], style: str, line_numbers: str | bool @@ -168,6 +188,7 @@ class App: return web.HTTPBadRequest(text="provide a key to fetch") if not await self.database.exists(key): + log_info(f"paste {key} was not found, returning 404") return web.HTTPNotFound() req = self.bucket.get(key) @@ -177,12 +198,13 @@ class App: if get.status == 200: data = await get.read() else: - print( + log_error( f"{self.bucket.endpoint} returned status ({get.status}) while fetching {key}" ) + return web.HTTPInternalServerError() except Exception as e: - print(f"failed to get {key} from s3: {e}", file=sys.stderr) + log_error(f"failed to get {key} from s3: {e}") return web.HTTPInternalServerError() def decompress(): @@ -191,13 +213,13 @@ class App: try: decompressed = await asyncio.to_thread(decompress) except Exception as e: - print(f"failed to decompress blob {key}: {e}", file=sys.stderr) + log_error(f"failed to decompress blob {key}: {e}") return web.HTTPInternalServerError() try: text = decompressed.decode() except Exception as e: - print(f"failed to decode blob: {key}: {e}", file=sys.stderr) + log_error(f"failed to decode blob: {key}: {e}") return web.HTTPInternalServerError() syntax = request.query.get("syntax") @@ -207,6 +229,8 @@ class App: style = self.config.default_style if raw is not None: + log_info(f"sending raw paste {key}") + return web.HTTPOk(text=text, content_type="text/plain") else: @@ -215,13 +239,19 @@ class App: highlighted = await asyncio.to_thread(render) + log_info( + f"sending rendered paste {key} with syntax {syntax} and style {style}" + ) + return web.HTTPOk(text=highlighted, content_type="text/html") async def upload(self, request: web.Request) -> web.Response: + syntax = request.query.get("syntax") + try: await self.vacuum() except Exception as e: - print(f"vacuum failed: {e}") + log_error(f"vacuum failed: {e}") return web.HTTPInternalServerError() match request.content_length: @@ -235,7 +265,7 @@ class App: try: data = await request.read() except Exception as e: - print(f"failed to read data: {e}", file=sys.stderr) + log_error(f"failed to read data: {e}") return web.HTTPInternalServerError(text="failed to read data") try: @@ -251,7 +281,7 @@ class App: try: compressed = await asyncio.to_thread(compress) except Exception as e: - print(f"failed to compress data: {e}", file=sys.stderr) + log_error(f"failed to compress data: {e}") return web.HTTPInternalServerError() key = generate_key(self.config.dictionary, self.config.key_length) @@ -268,25 +298,32 @@ class App: req.url, headers=req.headers, data=compressed ) as put: if put.status != 200: - print(f"failed to put {key} to bucket with status: {put.status}") + log_error( + f"failed to put {key} to bucket with status: {put.status}" + ) return web.HTTPInternalServerError() except Exception as e: - print(f"failed to put {key} to bucket: {e}") + log_error(f"failed to put {key} to bucket: {e}") return web.HTTPInternalServerError() try: await self.database.insert( - PasteRow( - key, datetime.now(UTC), len(compressed), request.query.get("syntax") - ) + PasteRow(key, datetime.now(UTC), len(compressed), syntax) ) except Exception as e: - print(f"failed to insert {key} into database: {e}", file=sys.stderr) + log_error(f"failed to insert {key} into database: {e}") return web.HTTPInternalServerError() - return web.HTTPOk(text=f"{self.config.site}/paste/{key}") + url = f"{self.config.site}/paste/{key}" + + log_info( + f"uploaded paste {key} with syntax {syntax} of size {len(compressed)} bytes: {url}" + ) + + return web.HTTPOk(text=url) async def vacuum(self): + log_info("starting vaccum") while ( use := await self.database.storage_use() ) is not None and use > self.config.s3_max_bytes: @@ -300,13 +337,12 @@ class App: async with aiohttp.ClientSession().delete( req.url, headers=req.headers ) as delete: - if delete.status != 200: - print( + if delete.status == 200: + log_info(f"successfully deleted {oldest}") + else: + log_warning( f"failed to delete {oldest}: got status {delete.status}", - file=sys.stderr, ) - else: - print(f"successfully deleted {oldest}", file=sys.stderr) await self.database.delete(oldest) @@ -412,9 +448,10 @@ async def main() -> int: if __name__ == "__main__": try: + log_info("starting pypaste") sys.exit(asyncio.run(main())) except KeyboardInterrupt: sys.exit(0) except Exception as e: - print(f"failure: {e}", file=sys.stderr) + log_error(str(e)) sys.exit(1) |