Beispiel #1
0
def LOGGER(filename):
    """creates a logger with the given name.

    You can use it as follows::

       log = cloudmesh.util.LOGGER(__file__)
       log.error("this is an error")
       log.info("this is an info")
       log.warning("this is a warning")

    """
    pwd = os.getcwd()
    name = filename.replace(pwd, "$PWD")
    try:
        (first, name) = name.split("site-packages")
        name += "... site"
    except:
        pass

    loglevel = logging.CRITICAL
    try:
        level = grep("loglevel:", config_file(
            "/cloudmesh_debug.yaml")).strip().split(":")[1].strip().lower()

        if level.upper() == "DEBUG":
            loglevel = logging.DEBUG
        elif level.upper() == "INFO":
            loglevel = logging.INFO
        elif level.upper() == "WARNING":
            loglevel = logging.WARNING
        elif level.upper() == "ERROR":
            loglevel = logging.ERROR
        else:
            level = logging.CRITICAL
    except:
        # print "LOGLEVEL NOT FOUND"
        loglevel = logging.DEBUG

    log = logging.getLogger(name)
    log.setLevel(loglevel)

    formatter = logging.Formatter(
        'CM {0:>50}:%(lineno)s: %(levelname)6s - %(message)s'.format(name))

    # formatter = logging.Formatter(
    #    'CM {0:>50}: %(levelname)6s - %(module)s:%(lineno)s %funcName)s: %(message)s'.format(name))
    handler = logging.StreamHandler()
    handler.setFormatter(formatter)
    log.addHandler(handler)
    return log
    },
                        prefix="cloudmesh.debug",
                        filename="./etc/cloudmesh_debug.yaml")

    print("PPRINT")
    print(70 * "=")
    pprint(config)

    print("PRINT")
    print(70 * "=")
    print(config.pprint())
    print(config.json())

    print(70 * "=")
    print("A =", config["a"])
    config.write(config_file("/d.yaml"), output="dict")
    config.write(config_file("/j.yaml"), output="json")
    config.write(config_file("/y.yaml"), output="yaml")

    # this does not work
    # config.write(config_file("/print.yaml"), output="print")

    print("mongo.path GET =", config.get("cloudmesh.server.mongo.path"))
    print("mongo.path ATTRIBUTE =", config.attribute("mongo.path"))

    print("get A =", config.get("a"))

    print("wrong mongo.path ATTRIBUTE =", config.attribute("mongo.path.wrong"))
    print("wrong mongo.path GET =",
          config.get("cloudmesh.server.mongo.path.wrong"))