Skip to content

ImportError raised when climada logging.managed is false #171

@spjuhel

Description

@spjuhel

Setting the following configuration for logging in climada configuration

"logging": {
        "managed": false
    }

Raises the following error when importing anything from petals due to CONSOLE not being defined:

ImportError                               Traceback (most recent call last)

     22 from pathlib import Path
     24 import climada
---> 25 import climada_petals.util.config
     26 from .util.constants import *
     28 REPO_DATA = {
     29     'climada_petals[/data/gdp_asset](http://localhost:8888/data/gdp_asset)': [
     30         SYSTEM_DIR.joinpath('GDP2Asset_converter_2.5arcmin.nc'),
   (...)     57     ],
     58 }

      1 """
      2 This file is part of CLIMADA.
      3 
   (...)     19 init util
     20 """
---> 21 from .config import *
     22 from .constants import *

     30 import climada
     31 import climada.util.config
---> 32 from climada.util.config import (
     33     Config, _fetch_conf, CONFIG_NAME, CONFIG, CONSOLE)
     35 LOGGER = logging.getLogger('climada_petals')
     36 LOGGER.propagate = False

ImportError: cannot import name 'CONSOLE' from 'climada.util.config'

This comes from the fact that config.py in climada core only defines CONSOLE if CONFIG.logging.managed is True:

# set climada style logging
if CONFIG.logging.managed.bool():
    LOGGER = logging.getLogger("climada")
    LOGGER.propagate = False
    FORMATTER = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    )
    CONSOLE = logging.StreamHandler(stream=sys.stdout)
    CONSOLE.setFormatter(FORMATTER)
    LOGGER.addHandler(CONSOLE)

    LOGGER.setLevel(getattr(logging, CONFIG.log_level.str()))

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting pull requestContribute by raising a pull request to resolve this issue!bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions