Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another way to set defaults #7

Closed
RomanKornev opened this issue Dec 10, 2018 · 4 comments
Closed

Another way to set defaults #7

RomanKornev opened this issue Dec 10, 2018 · 4 comments
Labels
bug Something isn't working

Comments

@RomanKornev
Copy link

There needs to be another way to set defaults.
Environmental variables do not work for everything on Windows.
For example:
If I do not want any color for INFO level, I cannot set environment variable LOGURU_INFO_COLOR="" because then it's disabled on Windows.
I cannot do:

_defaults.LOGURU_INFO_COLOR = ""

Since logger is already initialized.
Instead, I have to do extremely hacky stuff like:

from loguru import _defaults, logger
from loguru._logger import Level

logger.info('bold')
logger._levels['INFO'] = Level(_defaults.LOGURU_INFO_NO, "", _defaults.LOGURU_INFO_ICON)
logger.info('not bold')

Since Level is a namedtuple, I cannot even modify it in-place

Perhaps a config file or an .ini file as well would be better.

@Delgan
Copy link
Owner

Delgan commented Dec 10, 2018

Oh, that's an edge case I did not think about, thanks for reporting it!

I can see some ways to fix it without hacking inside Loguru internals.

Simply set the environment variable programmatically using os.environ before importing loguru:

import os
os.environ["LOGURU_INFO_COLOR"] = ""
from loguru import logger

But if you want to modify a Loguru default level from your script, better use the .level() method which also acts as a setter:

logger.level("INFO", color="")

This can also be done with .configure():

logger.configure(levels=[{"name": "INFO", "color": ""}])

However, this would mean that you have to repeat this inside all of your scripts. That is not very convenient at all. This is why environment variables are useful: set it once, and you're done.

Therefore, a workaround has to be found. I guess that if the string is just a space like setx LOGURU_INFO_COLOR=" " I could interpret that as if it was an empty string, and hence don't colorize anything.

@RomanKornev
Copy link
Author

setx LOGURU_INFO_COLOR " " as an empty string

I like that because currently it just inserts a space and breaks all formatting.

@Delgan
Copy link
Owner

Delgan commented Dec 12, 2018

The recently published 0.2.2 version should allow you to use an empty string for default color by setting a "spaces only" environment variable.

Don't hesitate to open a new issue if you encounter other troubles. 😉

@Delgan Delgan closed this as completed Dec 12, 2018
@RomanKornev
Copy link
Author

Thanks for the update @Delgan! Works without issues.

@Delgan Delgan added the bug Something isn't working label Mar 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants