Skip to content

Commit 40004b5

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add doc for logger usage"
2 parents ba825a4 + 0ac6b4f commit 40004b5

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

doc/source/command-logs.rst

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
============
2+
Command Logs
3+
============
4+
5+
Logger usage in OpenStackClient is not exactly the same as those in other
6+
OpenStack projects. The following basic rules should be followed.
7+
8+
1. OpenStackClient uses python standard logging library instead of oslo.log
9+
so that it will depend on oslo as little as possible.
10+
11+
2. All logs except debug log need to be translated. The log message strings
12+
that need to be translated should follow the rule of i18n guidelines:
13+
http://docs.openstack.org/developer/oslo.i18n/guidelines.html
14+
15+
3. There are mainly two kinds of logs in OpenStackClient: command specific
16+
log and general log. Use different logger to record them. The examples
17+
below will show the detail.
18+
19+
Command specific log
20+
====================
21+
22+
Command specific logs are those messages that used to record info, warning
23+
and error generated from a specific command. OpenStackClient uses the logger
24+
of the module the command belongs to to record the command specific logs.
25+
26+
Example
27+
~~~~~~~
28+
29+
This example shows how to log command specific logs in OpenStackClient.
30+
31+
.. code-block:: python
32+
33+
import logging
34+
35+
from openstackclient.i18n import _
36+
37+
38+
LOG = logging.getLogger(__name__) # Get the logger of this module
39+
40+
## ...
41+
42+
LOG.error(_("Error message"))
43+
LOG.warning(_("Warning message"))
44+
LOG.info(_("Info message"))
45+
LOG.debug("Debug message") # Debug messages do not need to be translated
46+
47+
## ...
48+
49+
General log
50+
===========
51+
52+
General logs are those messages that not specific to any single command. Use
53+
the logger of ``openstackclient.shell`` to record them. In each command class,
54+
we can simply get this logger by ``self.app.log``.
55+
56+
Example
57+
~~~~~~~
58+
59+
This example shows how to log general logs in OpenStackClient.
60+
61+
.. code-block:: python
62+
63+
from openstackclient.i18n import _
64+
65+
66+
## ...
67+
68+
self.app.log.error(_("Error message"))
69+
self.app.log.warning(_("Warning message"))
70+
self.app.log.info(_("Info message"))
71+
self.app.log.debug("Debug message") # Debug messages do not need to be translated
72+
73+
## ...

doc/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Developer Documentation
5151
command-options
5252
command-wrappers
5353
command-errors
54+
command-logs
5455
specs/commands
5556

5657
Project Goals

0 commit comments

Comments
 (0)