Skip to content

Disable colors when logging to a file and running outside Docker #458

@mejroslav

Description

@mejroslav

Logging to a file when running outside Docker

I found this issue when experimenting with logging to a file using the configuration from documentation:

When logging to a file outside Docker (running the app directly), escape color sequences are present. I tried to explicitly use the optionuse_colors=False here:

asab/asab/log.py

Lines 56 to 60 in 7c76f10

self.FileHandler.setFormatter(StructuredDataFormatter(
fmt=Config["logging:file"]["format"],
datefmt=Config["logging:file"]["datefmt"],
sd_id=Config["logging"]["sd_id"],
))

...but that didn't help.


The config:

[logging:file]
path=./output.log
format=%(asctime)s %(levelname)s %(name)s %(struct_data)s%(message)s,
datefmt=%d-%b-%Y %H:%M:%S.%f
backup_count=3
rotate_every=1d

The app:

#!/usr/bin/env python3
import logging
import asab

L = logging.getLogger(__name__)


class MyApplication(asab.Application):
	async def main(self):
		a, b = 24, 0
		try:
			print(a / b)
		except ZeroDivisionError:
			L.error("Division by zero.", struct_data={"a": a, "b": b})

		self.stop()


if __name__ == "__main__":
	app = MyApplication()
	app.run()

Output:

27-Jul-2023 17:18:39.572756 �[1;36mNOTICE�[0m asab.application is ready.,
27-Jul-2023 17:18:39.572942 �[1;31mERROR�[0m __main__ [sd a="24" b="0"] Division by zero.,
27-Jul-2023 17:18:39.572948 �[1;31mERROR�[0m root Invalid 'rotate_every' configuration value.,
27-Jul-2023 17:18:39.573116 �[1;36mNOTICE�[0m asab.application [sd exit_code="0"] is exiting ...,

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions