Comments (3)
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.
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.
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)
- No module named 'colorlog' HOT 1
- v4.8.0 - ColoredRecord does not set asctime property. HOT 2
- 5.0.0: test suite warnings HOT 2
- 'bold' option is not working HOT 1
- Typing information is missing on import. HOT 2
- ModuleNotFoundError: No module named 'colorlog' HOT 1
- Purple - PowerShell HOT 1
- hope add predefined level such as colorlog.ERROR colorlog.DEBUG and so on HOT 1
- [Bug] colorlog.basicConfig() doesn't seems to work when upgrading from v5 to v6 HOT 3
- colorlog introduces "[1m[0m"-like entries in logs running on argo workflows HOT 2
- Logging is sometimes duplicated without color HOT 5
- How to make `print()` statement output colorful?
- Doesn't support defaults HOT 1
- Document `light_*` colors? HOT 4
- Update default_log_colors from log_colors instead of replace it HOT 3
- Failed to add color to log HOT 4
- Docu like ReadTheDocs / Offering a objects.inv HOT 1
- Duplicative stream definition required due to design choice made in #64 HOT 1
- support *stacklevel* argument HOT 3
- colorlog.basicConfig does not accept '{' style HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-colorlog.