Giter Site home page Giter Site logo

Comments (3)

borntyping avatar borntyping commented on July 17, 2024

Could you provide an example of what you're trying to do or why it would be useful to output coloured messages to a file?

from python-colorlog.

brey avatar brey commented on July 17, 2024

It is not really about coloured output. I have added an additional file handler to the logger created by color log based on logging ...

import colorlog, logging

handler = colorlog.StreamHandler()
handler.setFormatter(colorlog.ColoredFormatter(
	'%(log_color)s%(levelname)s:%(name)s:%(message)s'))

logger = colorlog.getLogger('example')
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

file_handler = logging.StreamHandler('test.log')
fformatter = logging.Formatter('%(levelname)-8s %(asctime)s:%(name)s:%(message)s')
file_handler.setFormatter(fformatter)
logger.addHandler(file_handler)

The idea is to output on screen and to a file as the same time (it works with logging). However, when I run

logger.info('info')

I get

INFO:example:info
--- Logging error ---
Traceback (most recent call last):
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/logging/__init__.py", line 1037, in emit
    stream.write(msg + self.terminator)
AttributeError: 'str' object has no attribute 'write'
Call stack:
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/ipykernel/kernelapp.py", line 563, in start
    self.io_loop.start()
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/tornado/platform/asyncio.py", line 148, in start
    self.asyncio_loop.run_forever()
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/asyncio/base_events.py", line 539, in run_forever
    self._run_once()
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/asyncio/base_events.py", line 1775, in _run_once
    handle._run()
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/tornado/ioloop.py", line 690, in <lambda>
    lambda f: self._run_callback(functools.partial(callback, future))
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/tornado/ioloop.py", line 743, in _run_callback
    ret = callback()
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/tornado/gen.py", line 787, in inner
    self.run()
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/tornado/gen.py", line 748, in run
    yielded = self.gen.send(value)
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 365, in process_one
    yield gen.maybe_future(dispatch(*args))
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/tornado/gen.py", line 209, in wrapper
    yielded = next(result)
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 272, in dispatch_shell
    yield gen.maybe_future(handler(stream, idents, msg))
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/tornado/gen.py", line 209, in wrapper
    yielded = next(result)
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 542, in execute_request
    user_expressions, allow_stdin,
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/tornado/gen.py", line 209, in wrapper
    yielded = next(result)
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/ipykernel/ipkernel.py", line 294, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/ipykernel/zmqshell.py", line 536, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2855, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell
    return runner(coro)
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
    coro.send(None)
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3058, in run_cell_async
    interactivity=interactivity, compiler=compiler, result=result)
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "/Users/brey/miniconda3/envs/pyPoseidon/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-6-8d119e9a0d71>", line 1, in <module>
    logger.info('info')
Message: 'info'
Arguments: ()

.. so the screen part works but it can not output to file. I could use two different loggers but maybe there is a way to inherent the output functionality from logging...

from python-colorlog.

borntyping avatar borntyping commented on July 17, 2024

This is a problem with your use of the logging library, not colorlog.

In your example, you should use logging.FileHandler('test.log') instead of logging.StreamHandler(...).

FileHandler accepts a filename, but StreamHandler must be passed an open-file or file-like object. I hope this helps :)

from python-colorlog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.