@@ -25,6 +25,7 @@ defined by my verboselogs_ package: if you install both `coloredlogs` and
25
25
26
26
.. contents ::
27
27
:local:
28
+ :depth: 1
28
29
29
30
Format of log messages
30
31
----------------------
@@ -80,24 +81,49 @@ Colored output from cron
80
81
When `coloredlogs ` is used in a cron _ job, the output that's e-mailed to you by
81
82
cron won't contain any ANSI escape sequences because `coloredlogs ` realizes
82
83
that it's not attached to an interactive terminal. If you'd like to have colors
83
- e-mailed to you by cron there's a simple way to set it up::
84
+ e-mailed to you by cron there are two ways to make it happen:
85
+
86
+ .. contents ::
87
+ :local:
88
+
89
+ You can use this feature without using `coloredlogs ` in your Python modules,
90
+ but please note that only normal text, bold text and text with one of the
91
+ foreground colors black, red, green, yellow, blue, magenta, cyan and white
92
+ (these are the portable ANSI color codes) are supported.
93
+
94
+ Modifying your crontab
95
+ ~~~~~~~~~~~~~~~~~~~~~~
96
+
97
+ Here's an example of a minimal crontab::
84
98
85
99
MAILTO="your-email-address@here"
86
100
CONTENT_TYPE="text/html"
87
101
* * * * * root coloredlogs --to-html your-command
88
102
89
103
The ``coloredlogs `` program is installed when you install the `coloredlogs `
90
- package. When you execute ``coloredlogs --to-html your-command `` it runs
104
+ Python package. When you execute ``coloredlogs --to-html your-command `` it runs
91
105
``your-command `` under the external program ``script `` (you need to have this
92
106
installed). This makes ``your-command `` think that it's attached to an
93
107
interactive terminal which means it will output ANSI escape sequences which
94
108
will then be converted to HTML by the ``coloredlogs `` program. Yes, this is a
95
109
bit convoluted, but it works great :-)
96
110
97
- You can use this feature without using `coloredlogs ` in your Python modules,
98
- but please note that only normal text, bold text and text with one of the
99
- foreground colors black, red, green, yellow, blue, magenta, cyan and white
100
- (these are the portable ANSI color codes) are supported.
111
+ Modifying your Python code
112
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
113
+
114
+ The ColoredCronMailer _ class provides a context manager that automatically
115
+ enables HTML output when the ``$CONTENT_TYPE `` variable has been correctly set
116
+ in the crontab.
117
+
118
+ This requires my capturer _ package which you can install using ``pip install
119
+ 'coloredlogs[cron]' ``. The ``[cron] `` extra will pull in capturer _ 2.4 or newer
120
+ which is required to capture the output while silencing it - otherwise you'd
121
+ get duplicate output in the emails sent by ``cron ``.
122
+
123
+ The context manager can also be used to retroactively silence output that has
124
+ already been produced, this can be useful to avoid spammy cron jobs that have
125
+ nothing useful to do but still email their output to the system administrator
126
+ every few minutes :-).
101
127
102
128
Contact
103
129
-------
@@ -116,14 +142,16 @@ This software is licensed under the `MIT license`_.
116
142
117
143
118
144
.. External references:
119
- .. _ANSI escape sequences : http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
145
+ .. _ANSI escape sequences : https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
146
+ .. _capturer : https://pypi.python.org/pypi/capturer
120
147
.. _Colorama : https://pypi.python.org/pypi/colorama
121
- .. _ColoredFormatter : http://coloredlogs.readthedocs.io/en/latest/#coloredlogs.ColoredFormatter
148
+ .. _ColoredCronMailer : https://coloredlogs.readthedocs.io/en/latest/#coloredlogs.converter.ColoredCronMailer
149
+ .. _ColoredFormatter : https://coloredlogs.readthedocs.io/en/latest/#coloredlogs.ColoredFormatter
122
150
.. _cron : https://en.wikipedia.org/wiki/Cron
123
151
.. _GitHub : https://github.com/xolox/python-coloredlogs
124
- .. _logging.Formatter : http ://docs.python.org/2/library/logging.html#logging.Formatter
152
+ .. _logging.Formatter : https ://docs.python.org/2/library/logging.html#logging.Formatter
125
153
.. _logging : https://docs.python.org/2/library/logging.html
126
- .. _MIT license : http ://en.wikipedia.org/wiki/MIT_License
154
+ .. _MIT license : https ://en.wikipedia.org/wiki/MIT_License
127
155
.. _online documentation : https://coloredlogs.readthedocs.io/
128
156
129
157
.. _PyPI : https://pypi.python.org/pypi/coloredlogs
0 commit comments