Skip to content

Commit 7d8e2bc

Browse files
authored
Enables logging to a file (#8)
1 parent 2a709a1 commit 7d8e2bc

File tree

5 files changed

+173
-26
lines changed

5 files changed

+173
-26
lines changed

HISTORY.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
History
33
=======
44

5+
1.1.2 (2024-10-11)
6+
-------------------
7+
8+
* Enables logging to a file
9+
510
1.1.1 (2024-10-09)
611
-------------------
712

README.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,57 @@ Let's try to get a token and create a resource.::
7676
... )
7777
>>> myresource.resp.body // {"result":true...
7878

79+
80+
Settings
81+
---------
82+
83+
To customize the settings of this library, place the configuration file in one of the following locations.::
84+
85+
$PWD/k2hr3client.ini
86+
$HOME/.k2hr3client.ini
87+
/etc/antpickax/k2hr3client.ini
88+
89+
To use the settings in your program, see the following example.::
90+
91+
>>> from k2hr3client import CONFIG
92+
>>> print(CONFIG['DEFAULT']['iaas_user'])
93+
demo
94+
95+
Here are the default settings.
96+
97+
+---------+------------------------+-------------------------------------------------+------------------------+
98+
| section | key name | description | default value |
99+
+=========+========================+=================================================+========================+
100+
| DEFAULT | debug | Enable debug log if True | False |
101+
+---------+------------------------+-------------------------------------------------+------------------------+
102+
| DEFAULT | iaas_url | IaaS API URL(OpenStack) | http://172.24.4.1 |
103+
+---------+------------------------+-------------------------------------------------+------------------------+
104+
| DEFAULT | iaas_project | IaaS project name | demo |
105+
+---------+------------------------+-------------------------------------------------+------------------------+
106+
| DEFAULT | iaas_user | IaaS user name | demo |
107+
+---------+------------------------+-------------------------------------------------+------------------------+
108+
| DEFAULT | iaas_password | IaaS password of the user | password |
109+
+---------+------------------------+-------------------------------------------------+------------------------+
110+
| DEFAULT | log_file | log file name | sys.stderr |
111+
+---------+------------------------+-------------------------------------------------+------------------------+
112+
| DEFAULT | log_dir | log directory name | logs |
113+
+---------+------------------------+-------------------------------------------------+------------------------+
114+
| DEFAULT | log_level | log level(error,warn,info,debug,notset) | info |
115+
+---------+------------------------+-------------------------------------------------+------------------------+
116+
| k2hr3 | api_url | k2hr3 API URL | http://127.0.0.1:18080 |
117+
+---------+------------------------+-------------------------------------------------+------------------------+
118+
| k2hr3 | api_version | k2hr3 api version | v1 |
119+
+---------+------------------------+-------------------------------------------------+------------------------+
120+
| http | timeout_seconds | timeout to establish tcp connections | 30 |
121+
+---------+------------------------+-------------------------------------------------+------------------------+
122+
| http | retry_interval_seconds | interval to reconnect with HTTP server | 60 |
123+
+---------+------------------------+-------------------------------------------------+------------------------+
124+
| http | max_retries | maximum count to reconnect with HTTP server | 3 |
125+
+---------+------------------------+-------------------------------------------------+------------------------+
126+
| http | allow_self_signed_cert | Allow to use self-signed certification in HTTPS | False |
127+
+---------+------------------------+-------------------------------------------------+------------------------+
128+
129+
79130
Development
80131
------------
81132

conf/k2hr3client.ini

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# k2hr3client - Python client for K2HR3 REST API
4+
#
5+
# Copyright 2024 LY Corporation
6+
#
7+
# K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers
8+
# common management information for the cloud.
9+
# K2HR3 can dynamically manage information as "who", "what", "operate".
10+
# These are stored as roles, resources, policies in K2hdkc, and the
11+
# client system can dynamically read and modify these information.
12+
#
13+
# For the full copyright and license information, please view
14+
# the license file that was distributed with this source code.
15+
#
16+
# AUTHOR: Hirotaka Wakabayashi
17+
# CREATE: Fri Oct 11 2024
18+
# REVISION:
19+
#
20+
#
121
[DEFAULT]
222
debug = False
3-
iaas_url = "http://172.24.4.1"
4-
iaas_project = "demo"
5-
iaas_user = "demo"
6-
iaas_password = "password"
23+
iaas_url = http://172.24.4.1
24+
iaas_project = demo
25+
iaas_user = demo
26+
iaas_password = password
727
log_file = sys.stdout
828
log_dir = logs
929
log_level = logging.INFO
1030

1131
[k2hr3]
12-
api_url = "http://127.0.0.1:18080"
13-
api_version = "v1"
32+
api_url = http://127.0.0.1:18080
33+
api_version = v1
1434

1535
[http]
1636
timeout_seconds = 30
1737
retry_interval_seconds = 60
1838
max_retries = 3
1939
allow_self_signed_cert = True
40+
41+
#
42+
# Local variables:
43+
# tab-width: 4
44+
# c-basic-offset: 4
45+
# End:
46+
# vim600: expandtab sw=4 ts=4 fdm=marker
47+
# vim<600: expandtab sw=4 ts=4
48+
#

docs/locale/ja/LC_MESSAGES/history.po

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ msgid ""
1010
msgstr ""
1111
"Project-Id-Version: k2hr3client \n"
1212
"Report-Msgid-Bugs-To: \n"
13-
"POT-Creation-Date: 2024-10-09 15:10+0900\n"
13+
"POT-Creation-Date: 2024-10-11 16:22+0900\n"
1414
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1515
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1616
"Language: ja\n"
@@ -26,74 +26,82 @@ msgid "History"
2626
msgstr "歴史"
2727

2828
#: ../../../HISTORY.rst:6
29-
msgid "1.1.1 (2024-10-09)"
29+
msgid "1.1.2 (2024-10-11)"
3030
msgstr ""
3131

3232
#: ../../../HISTORY.rst:8
33+
msgid "Enables logging to a file"
34+
msgstr "ログファイルへのログ出力をサポート"
35+
36+
#: ../../../HISTORY.rst:10
37+
msgid "1.1.1 (2024-10-09)"
38+
msgstr ""
39+
40+
#: ../../../HISTORY.rst:13
3341
msgid "Removes hardcodings"
3442
msgstr "ハードコーディングを削除"
3543

36-
#: ../../../HISTORY.rst:9
44+
#: ../../../HISTORY.rst:14
3745
msgid "Adds Configuration Interface"
3846
msgstr "設定用インターフェースを追加"
3947

40-
#: ../../../HISTORY.rst:10 ../../../HISTORY.rst:17
48+
#: ../../../HISTORY.rst:15 ../../../HISTORY.rst:22
4149
msgid "Updates the docs"
4250
msgstr "ドキュメントの更新"
4351

44-
#: ../../../HISTORY.rst:13
52+
#: ../../../HISTORY.rst:18
4553
msgid "1.1.0 (2024-10-04)"
4654
msgstr ""
4755

48-
#: ../../../HISTORY.rst:15
56+
#: ../../../HISTORY.rst:20
4957
msgid "Removes the deprecated Interfaces"
5058
msgstr "非推奨のインタフェースを削除"
5159

52-
#: ../../../HISTORY.rst:16
60+
#: ../../../HISTORY.rst:21
5361
msgid "Removes the examples"
5462
msgstr "例示プログラムの削除"
5563

56-
#: ../../../HISTORY.rst:20
64+
#: ../../../HISTORY.rst:25
5765
msgid "1.0.3 (2024-10-03)"
5866
msgstr ""
5967

60-
#: ../../../HISTORY.rst:22
68+
#: ../../../HISTORY.rst:27
6169
msgid "Fixes the K2hr3 API version"
6270
msgstr "K2hr3 APIのバージョンを固定化"
6371

64-
#: ../../../HISTORY.rst:23
72+
#: ../../../HISTORY.rst:28
6573
msgid "Deprecates K2hr3Resource.create_conf_resource params"
6674
msgstr "K2hr3Resource.create_conf_resourceのパラメータを一部非推奨化"
6775

68-
#: ../../../HISTORY.rst:26
76+
#: ../../../HISTORY.rst:31
6977
msgid "1.0.2 (2024-09-26)"
7078
msgstr ""
7179

72-
#: ../../../HISTORY.rst:28
80+
#: ../../../HISTORY.rst:33
7381
msgid "Fixes lint errors"
7482
msgstr "静的解析ツールのエラーを修正"
7583

76-
#: ../../../HISTORY.rst:31
84+
#: ../../../HISTORY.rst:36
7785
msgid "1.0.1 (2024-09-25)"
7886
msgstr ""
7987

80-
#: ../../../HISTORY.rst:33
88+
#: ../../../HISTORY.rst:38
8189
msgid "Fixes resource API bugs"
8290
msgstr "Resource APIの不具合を修正"
8391

84-
#: ../../../HISTORY.rst:36
92+
#: ../../../HISTORY.rst:41
8593
msgid "1.0.0 (2024-08-28)"
8694
msgstr ""
8795

88-
#: ../../../HISTORY.rst:38
96+
#: ../../../HISTORY.rst:43
8997
msgid "Supports the other APIs"
9098
msgstr "他のAPIをサポート"
9199

92-
#: ../../../HISTORY.rst:41
100+
#: ../../../HISTORY.rst:46
93101
msgid "0.0.1 (2020-08-28)"
94102
msgstr ""
95103

96-
#: ../../../HISTORY.rst:43
104+
#: ../../../HISTORY.rst:48
97105
msgid "Supports Token, Resource, Policy and Role API"
98106
msgstr "トークン、リソース、ポリシーおよびロールAPIをサポート"
99107

src/k2hr3client/__init__.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@
2222
"""K2HR3 Python Client of Token API."""
2323

2424
__author__ = 'Hirotaka Wakabayashi <hiwakaba@lycorp.co.jp>'
25-
__version__ = '1.1.1'
25+
__version__ = '1.1.2'
2626

2727
import configparser
28+
import logging
29+
from logging.handlers import TimedRotatingFileHandler
30+
from logging import StreamHandler
2831
from pathlib import Path
2932
import sys
3033

34+
LOG = logging.getLogger(__name__)
35+
3136
if sys.platform.startswith('win'):
3237
raise ImportError(r'Currently we do not test well on windows')
3338

@@ -61,7 +66,7 @@ def get_version() -> str:
6166
default_section['iaas_password'] = "password"
6267
default_section['log_file'] = "sys.stderr"
6368
default_section['log_dir'] = "logs"
64-
default_section['log_level'] = "logging.INFO"
69+
default_section['log_level'] = "info"
6570

6671
# [k2hr3]
6772
# api_url = "http://127.0.0.1:18080"
@@ -100,6 +105,55 @@ def get_version() -> str:
100105
CONFIG.read(my_config.absolute())
101106
break
102107

108+
# 3. Configure logger
109+
# 3.1. loglevel
110+
_nametolevel = {
111+
'error': logging.ERROR,
112+
'warn': logging.WARNING,
113+
'info': logging.INFO,
114+
'debug': logging.DEBUG,
115+
'notset': logging.NOTSET
116+
}
117+
LOG_LEVEL = logging.INFO # noqa
118+
if CONFIG['DEFAULT'].get('log_level'):
119+
LOG_LEVEL = _nametolevel.get(
120+
CONFIG['DEFAULT'].get('log_level'),
121+
logging.INFO)
122+
LOG.setLevel(LOG_LEVEL)
123+
124+
# 3.2. format
125+
formatter = logging.Formatter(
126+
'%(asctime)-15s %(levelname)s %(name)s:%(lineno)d %(message)s')
127+
# 3.3. handler
128+
# Add the log message handler to the logger
129+
if CONFIG['DEFAULT'].get('log_file') == 'sys.stdout':
130+
stream_handler = StreamHandler(sys.stdout)
131+
LOG.addHandler(stream_handler)
132+
stream_handler.setFormatter(formatter)
133+
elif CONFIG['DEFAULT'].get('log_file') == 'sys.stderr':
134+
stream_handler = StreamHandler(sys.stderr) # default
135+
LOG.addHandler(stream_handler)
136+
stream_handler.setFormatter(formatter)
137+
138+
# log_dir
139+
log_dir_path = Path('.') # default
140+
log_dir = CONFIG['DEFAULT'].get('log_dir')
141+
if log_dir:
142+
log_dir_path = Path(log_dir)
143+
log_dir_path.mkdir(parents=True, exist_ok=True)
144+
145+
# log_file
146+
log_file = CONFIG['DEFAULT'].get('log_file')
147+
if log_file:
148+
log_file_path = log_dir_path.joinpath(log_file)
149+
log_file_path.touch()
150+
handler = TimedRotatingFileHandler(
151+
log_file_path.absolute(),
152+
when='midnight', encoding='UTF-8',
153+
backupCount=31)
154+
handler.setFormatter(formatter)
155+
LOG.addHandler(handler)
156+
103157
#
104158
# Local variables:
105159
# tab-width: 4

0 commit comments

Comments
 (0)