pythonloggingdebiansystemctl

How can I enable colors in debian systemd service


I have created a program who logs some informations in the given format :

[   INFO   ] [<path>/main_platform/__init__.py:handle:50] Le monde a été enregistré avec succès
[   INFO   ] [<path>/main_platform/server/__init__.py:run:38] Le serveur est en train de démarrer
[   INFO   ] [<path>/main_platform/server/clients/__init__.py:onOpen:55] Le client 1 s'est connecté

with the "INFO" text in bold green, in the /var/log/serviceName/output.log file.

Since I created a service like that :

[Unit]
Description=<My service description>
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=1
User=root
ExecStart=<pathToService>
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

and used it to start my server, the "INFO" stylings were gone.

After some researches, I copyied my ~/.bashrc file to /root/.bashrc. Now, when I make a sudo -i, colors are enabled. But even after a total reboot, the logs of the service are still unstyled.

I am using the termcolor module in python on debian 10.3.

How can I enable the colors?

EDIT :

After some researches, it seems that the problem comes from the systemctl environment and not from the root user.


Solution

  • I can use force_color with termcolor.colored() :

    print(termcolor.colored("INFO", 'green', attrs = ("bold",), force_color=True))
    

    should do the staff.

    Github reference