Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Inconsistent output in console and logfile. #285

Open
meet-minimalist opened this issue Mar 25, 2020 · 0 comments
Open

Inconsistent output in console and logfile. #285

meet-minimalist opened this issue Mar 25, 2020 · 0 comments

Comments

@meet-minimalist
Copy link

  • logzero version: 1.5.0
  • Python version: 3.6.8
  • Operating System: Windows 10

Description

I have ran below code block with updated data formatting. When executing the code block, the data formatting have been updated in console, but the logfile which have been written still show the default formatting. I tried to get it done with another code block (code block-2). It shows updated date formatting in console as well as logfile. But in logfile it prints extra character for color which I dont want.

What I Did

Code Block - 1

import logzero
from logzero import logger

log_format = '%(color)s[%(asctime)s %(module)s:%(lineno)d]%(end_color)s %(message)s'
formatter = logzero.LogFormatter(fmt=log_format, datefmt='%d-%m-%Y %H:%M:%S')
logzero.setup_default_logger(formatter=formatter)

logzero.logfile('tp.txt')

logger.info("Hello 1")
logger.info("Hello 2")
logger.info("Hello 3")
logger.info("Hello 4")
Console Output

[25-03-2020 22:36:56 tp:19] Hello 1
[25-03-2020 22:36:56 tp:20] Hello 2
[25-03-2020 22:36:56 tp:21] Hello 3
[25-03-2020 22:36:56 tp:22] Hello 4

Logfile Output

[I 200325 22:36:56 tp:19] Hello 1
[I 200325 22:36:56 tp:20] Hello 2
[I 200325 22:36:56 tp:21] Hello 3
[I 200325 22:36:56 tp:22] Hello 4

Code Block - 2

import logzero

log_format = '%(color)s[%(asctime)s %(module)s:%(lineno)d]%(end_color)s %(message)s'
formatter = logzero.LogFormatter(fmt=log_format, datefmt='%d-%m-%Y %H:%M:%S')

logger_1 = logzero.setup_logger(logfile='tp.txt', formatter=formatter)

logger_1.info("Hello 1")
logger_1.info("Hello 2")
logger_1.info("Hello 3")
logger_1.info("Hello 4")
Console Output

[25-03-2020 22:39:29 tp:17] Hello 1
[25-03-2020 22:39:29 tp:18] Hello 2
[25-03-2020 22:39:29 tp:19] Hello 3
[25-03-2020 22:39:29 tp:20] Hello 4

Logfile Output

�[32m[25-03-2020 22:39:29 tp:17]�[39m Hello 1
�[32m[25-03-2020 22:39:29 tp:18]�[39m Hello 2
�[32m[25-03-2020 22:39:29 tp:19]�[39m Hello 3
�[32m[25-03-2020 22:39:29 tp:20]�[39m Hello 4

meet-minimalist added a commit to meet-minimalist/logzero that referenced this issue Mar 26, 2020
The formatter that have been supplied sets the formatter only for console and if it sets the same for logfile, it prints extra color character which is unwanted. 

Basically when we supply formatter, the formatter only gets applied to console not the logfile (if logfile have been provided). That makes the output inconsistent.


Ref. of Issue:
metachris#285
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant