Skip to content
Snippets Groups Projects
README.md 1.38 KiB
Newer Older
  • Learn to ignore specific revisions
  • Art's avatar
    Art committed
    # Development Utils
    
    
    Art's avatar
    Art committed
    # Colored logger
    
    Art's avatar
    Art committed
    
    #### What?
    Bunch of patches for python logging:
      - add colors
    
    Art's avatar
    Art committed
      - change `stderr` stream references to `stdout`. Weirdly enough `stderr` is the default logging stream for everything, most configurations (including default Django) don't care to change it.
    
    Art's avatar
    Art committed
    
    #### Why?
    
    Art's avatar
    Art committed
    Because you don't want to see everything in red in your IDE.
    
    #### Safe?
    Quite safe. The downloaded file is only executed if its SHA256 matches the expected value. Patched logging also behaves very stable, time tested.
    
    Art's avatar
    Art committed
    
    #### How?
    Add the following piece of code:
      - in django - to your `settings/dev.py` (or wherever your dev settings are)
      - in other projects - anywhere where it will be executed somewhat early (like top of the file)
    
    
    Art's avatar
    Art committed
    ```python
    
    Art's avatar
    Art committed
    try:  # Colored logger CaaS. Auto downloaded and verified.
        import os
        import hashlib
        from urllib import request
    
    Art's avatar
    Art committed
        cache, url = "/tmp/_colored_logger.py", "https://lab.it.hs-hannover.de/lukyanch/pydevutils/raw/fa4af6555a8eb996e1be158ca12691de9b33ba45/colored_logger.py"
    
    Art's avatar
    Art committed
        code = bool(os.path.exists(cache) or request.urlretrieve(url, cache)) and open(cache, "r").read()
    
    Art's avatar
    Art committed
        assert hashlib.sha256(code.encode()).hexdigest() == "d4d261a40f95733f9fb184fc4ccb55d007b880cb62c8d6a06824d43eeb1391ac", "unrecognized content in " + cache
    
    Art's avatar
    Art committed
        exec(code)
    except Exception as e:
        print("No colored logger: {e.__class__.__name__}: {e}".format(e=e))
    ```