ic-python-logging 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Smart Social Contracts
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,5 @@
1
+ include LICENSE
2
+ include README.md
3
+ include requirements-dev.txt
4
+ include pyproject.toml
5
+ include setup.cfg
@@ -0,0 +1,193 @@
1
+ Metadata-Version: 2.4
2
+ Name: ic_python_logging
3
+ Version: 0.3.0
4
+ Summary: A lightweight logging library for the Internet Computer (IC)
5
+ Home-page: https://github.com/smart-social-contracts/ic-python-logging
6
+ Author: Smart Social Contracts
7
+ Author-email: Smart Social Contracts <smartsocialcontracts@gmail.com>
8
+ License: MIT License
9
+
10
+ Copyright (c) 2025 Smart Social Contracts
11
+
12
+ Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ of this software and associated documentation files (the "Software"), to deal
14
+ in the Software without restriction, including without limitation the rights
15
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ copies of the Software, and to permit persons to whom the Software is
17
+ furnished to do so, subject to the following conditions:
18
+
19
+ The above copyright notice and this permission notice shall be included in all
20
+ copies or substantial portions of the Software.
21
+
22
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ SOFTWARE.
29
+
30
+ Project-URL: Homepage, https://github.com/smart-social-contracts/ic-python-logging
31
+ Project-URL: Repository, https://github.com/smart-social-contracts/ic-python-logging.git
32
+ Project-URL: Issues, https://github.com/smart-social-contracts/ic-python-logging/issues
33
+ Keywords: logging,debugging,ic,internet-computer
34
+ Classifier: Development Status :: 4 - Beta
35
+ Classifier: Intended Audience :: Developers
36
+ Classifier: License :: OSI Approved :: MIT License
37
+ Classifier: Operating System :: OS Independent
38
+ Classifier: Programming Language :: Python :: 3
39
+ Classifier: Programming Language :: Python :: 3.10
40
+ Classifier: Topic :: Database
41
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
42
+ Requires-Python: >=3.7
43
+ Description-Content-Type: text/markdown
44
+ License-File: LICENSE
45
+ Dynamic: author
46
+ Dynamic: home-page
47
+ Dynamic: license-file
48
+ Dynamic: requires-python
49
+
50
+ # IC Python Logging
51
+
52
+ [![Test IC](https://github.com/smart-social-contracts/ic-python-logging/actions/workflows/test_ic.yml/badge.svg)](https://github.com/smart-social-contracts/ic-python-logging/actions)
53
+ [![Test](https://github.com/smart-social-contracts/ic-python-logging/actions/workflows/test.yml/badge.svg)](https://github.com/smart-social-contracts/ic-python-logging/actions)
54
+ [![PyPI version](https://badge.fury.io/py/ic-python-logging.svg)](https://badge.fury.io/py/ic-python-logging)
55
+ [![Python 3.10](https://img.shields.io/badge/python-3.10-blue.svg)](https://www.python.org/downloads/release/python-3107/)
56
+ [![License](https://img.shields.io/github/license/smart-social-contracts/ic-python-logging.svg)](https://github.com/smart-social-contracts/ic-python-logging/blob/main/LICENSE)
57
+
58
+ A simple logging system for the [Internet Computer](https://internetcomputer.org). Forked from [kybra-simple-logging](https://github.com/smart-social-contracts/kybra-simple-logging). The library includes in-memory log storage capabilities, providing robust logging for all canister functions including asynchronous operations.
59
+
60
+
61
+ ## Features
62
+
63
+ - CLI tool for querying logs directly from canisters to your local machine (including in semi-real time with `--follow`)
64
+ - Works seamlessly in both Internet Computer and non-IC environments
65
+ - Avoids using Python's standard logging module (which has compatibility issues in the IC environment)
66
+ - Named loggers with `get_logger()` function similar to Python's standard library
67
+ - Support for level-based filtering (DEBUG, INFO, WARNING, ERROR, CRITICAL)
68
+ - Global and per-logger log level configuration
69
+ - Ability to enable/disable logging completely
70
+ - Circular buffer to store logs in memory without exhausting memory
71
+
72
+
73
+ ## Installation
74
+
75
+ ```bash
76
+ pip install ic-python-logging
77
+ ```
78
+
79
+ ## Quick Start
80
+
81
+ ```python
82
+ from ic_python_logging import get_logger
83
+
84
+ # Create a logger
85
+ logger = get_logger("my_canister")
86
+
87
+ # Log messages at a specific level
88
+ logger.info("This is an info message")
89
+
90
+ # Set log level for a specific logger
91
+ logger.set_level(LogLevel.DEBUG)
92
+
93
+ # Use in-memory logging to retrieve logs
94
+ from ic_python_logging import get_logs, clear_logs, enable_memory_logging, disable_memory_logging
95
+
96
+ # Retrieve only ERROR logs
97
+ error_logs = get_logs(min_level="ERROR")
98
+
99
+ # Filter logs by logger name
100
+ component_logs = get_logs(logger_name="my_component")
101
+ ```
102
+
103
+ ## CLI Tool
104
+
105
+ The package includes a command-line tool for querying logs from canisters.
106
+ Example:
107
+
108
+ ```bash
109
+ # View the first 10 ERROR log entries of logger with name MY_LOGGER_NAME, and then follow and poll every 5 seconds, from the canister with ID <CANISTER_ID> on the IC network
110
+ kslog <CANISTER_ID> --tail 10 --level ERROR --name MY_LOGGER_NAME --follow --ic --interval 5
111
+ ```
112
+
113
+ To use this `kslog` with your canister, expose the query function:
114
+
115
+ ```python
116
+ # ##### Import Basilisk and the internal function #####
117
+
118
+ from basilisk import Opt, Record, Vec, nat, query
119
+ from ic_python_logging import get_canister_logs as _get_canister_logs
120
+
121
+
122
+ # Define the PublicLogEntry class directly in the test canister
123
+ class PublicLogEntry(Record):
124
+ timestamp: nat
125
+ level: str
126
+ logger_name: str
127
+ message: str
128
+ id: nat
129
+
130
+
131
+ @query
132
+ def get_canister_logs(
133
+ from_entry: Opt[nat] = None,
134
+ max_entries: Opt[nat] = None,
135
+ min_level: Opt[str] = None,
136
+ logger_name: Opt[str] = None,
137
+ ) -> Vec[PublicLogEntry]:
138
+ """
139
+ Re-export the get_canister_logs query function from the library
140
+ This makes it accessible as a query method on the test canister
141
+ """
142
+ logs = _get_canister_logs(
143
+ from_entry=from_entry,
144
+ max_entries=max_entries,
145
+ min_level=min_level,
146
+ logger_name=logger_name
147
+ )
148
+
149
+ # Convert the logs to our local PublicLogEntry type
150
+ return [
151
+ PublicLogEntry(
152
+ timestamp=log["timestamp"],
153
+ level=log["level"],
154
+ logger_name=log["logger_name"],
155
+ message=log["message"],
156
+ id=log["id"],
157
+ )
158
+ for log in logs
159
+ ]
160
+ ```
161
+
162
+ ## Development
163
+
164
+ ```bash
165
+ # Clone the repository
166
+ git clone https://github.com/smart-social-contracts/ic-python-logging.git
167
+ cd ic-python-logging
168
+
169
+ # Recommended setup
170
+ pyenv install 3.10.7
171
+ pyenv local 3.10.7
172
+ python -m venv venv
173
+ source venv/bin/activate
174
+ pip install ic-basilisk
175
+ python -m basilisk install-dfx-extension
176
+
177
+ # Install development dependencies
178
+ pip install -r requirements-dev.txt
179
+
180
+ # Run linters
181
+ ./run_linters.sh
182
+
183
+ # Run tests
184
+ cd tests && ./run_test.sh
185
+ ```
186
+
187
+ ## Contributing
188
+
189
+ Contributions are welcome! Please feel free to submit a Pull Request.
190
+
191
+ ## License
192
+
193
+ MIT
@@ -0,0 +1,144 @@
1
+ # IC Python Logging
2
+
3
+ [![Test IC](https://github.com/smart-social-contracts/ic-python-logging/actions/workflows/test_ic.yml/badge.svg)](https://github.com/smart-social-contracts/ic-python-logging/actions)
4
+ [![Test](https://github.com/smart-social-contracts/ic-python-logging/actions/workflows/test.yml/badge.svg)](https://github.com/smart-social-contracts/ic-python-logging/actions)
5
+ [![PyPI version](https://badge.fury.io/py/ic-python-logging.svg)](https://badge.fury.io/py/ic-python-logging)
6
+ [![Python 3.10](https://img.shields.io/badge/python-3.10-blue.svg)](https://www.python.org/downloads/release/python-3107/)
7
+ [![License](https://img.shields.io/github/license/smart-social-contracts/ic-python-logging.svg)](https://github.com/smart-social-contracts/ic-python-logging/blob/main/LICENSE)
8
+
9
+ A simple logging system for the [Internet Computer](https://internetcomputer.org). Forked from [kybra-simple-logging](https://github.com/smart-social-contracts/kybra-simple-logging). The library includes in-memory log storage capabilities, providing robust logging for all canister functions including asynchronous operations.
10
+
11
+
12
+ ## Features
13
+
14
+ - CLI tool for querying logs directly from canisters to your local machine (including in semi-real time with `--follow`)
15
+ - Works seamlessly in both Internet Computer and non-IC environments
16
+ - Avoids using Python's standard logging module (which has compatibility issues in the IC environment)
17
+ - Named loggers with `get_logger()` function similar to Python's standard library
18
+ - Support for level-based filtering (DEBUG, INFO, WARNING, ERROR, CRITICAL)
19
+ - Global and per-logger log level configuration
20
+ - Ability to enable/disable logging completely
21
+ - Circular buffer to store logs in memory without exhausting memory
22
+
23
+
24
+ ## Installation
25
+
26
+ ```bash
27
+ pip install ic-python-logging
28
+ ```
29
+
30
+ ## Quick Start
31
+
32
+ ```python
33
+ from ic_python_logging import get_logger
34
+
35
+ # Create a logger
36
+ logger = get_logger("my_canister")
37
+
38
+ # Log messages at a specific level
39
+ logger.info("This is an info message")
40
+
41
+ # Set log level for a specific logger
42
+ logger.set_level(LogLevel.DEBUG)
43
+
44
+ # Use in-memory logging to retrieve logs
45
+ from ic_python_logging import get_logs, clear_logs, enable_memory_logging, disable_memory_logging
46
+
47
+ # Retrieve only ERROR logs
48
+ error_logs = get_logs(min_level="ERROR")
49
+
50
+ # Filter logs by logger name
51
+ component_logs = get_logs(logger_name="my_component")
52
+ ```
53
+
54
+ ## CLI Tool
55
+
56
+ The package includes a command-line tool for querying logs from canisters.
57
+ Example:
58
+
59
+ ```bash
60
+ # View the first 10 ERROR log entries of logger with name MY_LOGGER_NAME, and then follow and poll every 5 seconds, from the canister with ID <CANISTER_ID> on the IC network
61
+ kslog <CANISTER_ID> --tail 10 --level ERROR --name MY_LOGGER_NAME --follow --ic --interval 5
62
+ ```
63
+
64
+ To use this `kslog` with your canister, expose the query function:
65
+
66
+ ```python
67
+ # ##### Import Basilisk and the internal function #####
68
+
69
+ from basilisk import Opt, Record, Vec, nat, query
70
+ from ic_python_logging import get_canister_logs as _get_canister_logs
71
+
72
+
73
+ # Define the PublicLogEntry class directly in the test canister
74
+ class PublicLogEntry(Record):
75
+ timestamp: nat
76
+ level: str
77
+ logger_name: str
78
+ message: str
79
+ id: nat
80
+
81
+
82
+ @query
83
+ def get_canister_logs(
84
+ from_entry: Opt[nat] = None,
85
+ max_entries: Opt[nat] = None,
86
+ min_level: Opt[str] = None,
87
+ logger_name: Opt[str] = None,
88
+ ) -> Vec[PublicLogEntry]:
89
+ """
90
+ Re-export the get_canister_logs query function from the library
91
+ This makes it accessible as a query method on the test canister
92
+ """
93
+ logs = _get_canister_logs(
94
+ from_entry=from_entry,
95
+ max_entries=max_entries,
96
+ min_level=min_level,
97
+ logger_name=logger_name
98
+ )
99
+
100
+ # Convert the logs to our local PublicLogEntry type
101
+ return [
102
+ PublicLogEntry(
103
+ timestamp=log["timestamp"],
104
+ level=log["level"],
105
+ logger_name=log["logger_name"],
106
+ message=log["message"],
107
+ id=log["id"],
108
+ )
109
+ for log in logs
110
+ ]
111
+ ```
112
+
113
+ ## Development
114
+
115
+ ```bash
116
+ # Clone the repository
117
+ git clone https://github.com/smart-social-contracts/ic-python-logging.git
118
+ cd ic-python-logging
119
+
120
+ # Recommended setup
121
+ pyenv install 3.10.7
122
+ pyenv local 3.10.7
123
+ python -m venv venv
124
+ source venv/bin/activate
125
+ pip install ic-basilisk
126
+ python -m basilisk install-dfx-extension
127
+
128
+ # Install development dependencies
129
+ pip install -r requirements-dev.txt
130
+
131
+ # Run linters
132
+ ./run_linters.sh
133
+
134
+ # Run tests
135
+ cd tests && ./run_test.sh
136
+ ```
137
+
138
+ ## Contributing
139
+
140
+ Contributions are welcome! Please feel free to submit a Pull Request.
141
+
142
+ ## License
143
+
144
+ MIT
@@ -0,0 +1,38 @@
1
+ # Debug variable storage functions
2
+ # New in-memory logging functions
3
+ from ._handler import Level # Enum for log levels
4
+ from ._handler import LogEntry # Log entry data class
5
+ from ._handler import SimpleLogger # The logger class itself
6
+ from ._handler import clear_logs # Function to clear all logs from memory
7
+ from ._handler import disable_logging # Function to disable all logging
8
+ from ._handler import disable_memory_logging # Function to disable in-memory logging
9
+ from ._handler import enable_logging # Function to re-enable logging
10
+ from ._handler import enable_memory_logging # Function to enable in-memory logging
11
+ from ._handler import get_logger # Function to get a named logger
12
+ from ._handler import get_logs # Function to retrieve logs from memory
13
+ from ._handler import list_vars # Function to list all saved variables
14
+ from ._handler import load_var # Function to load a saved variable
15
+ from ._handler import logger # Default logger for backwards compatibility
16
+ from ._handler import save_var # Function to save a variable for debugging
17
+ from ._handler import set_log_level # Function to set log level for one or all loggers
18
+ from ._handler import set_max_log_entries # Function to set maximum log storage size
19
+ from ._handler import ( # Function to check memory logging status
20
+ is_memory_logging_enabled,
21
+ )
22
+
23
+ # New canister query function for exposing logs
24
+ try:
25
+ from ._handler import PublicLogEntry # Public log entry type for canister queries
26
+ from ._handler import ( # Query function to expose logs via canister query
27
+ get_canister_logs,
28
+ )
29
+ except ImportError:
30
+ # If CDK isn't available, these imports will fail
31
+ # This allows the library to be used in non-IC environments
32
+ pass
33
+
34
+ # This allows imports like:
35
+ # from ic_python_logging import logger, get_logger, set_log_level
36
+ # from ic_python_logging import save_var, load_var, list_vars
37
+ # from ic_python_logging import get_logs, clear_logs, set_max_log_entries, enable_memory_logging, disable_memory_logging
38
+ # from ic_python_logging import PublicLogEntry, get_canister_logs
@@ -0,0 +1,36 @@
1
+ """CDK compatibility layer.
2
+
3
+ This module centralizes all imports from the Internet Computer CDK (currently Basilisk).
4
+ To switch CDKs, only this file needs to be modified.
5
+ """
6
+
7
+ HAS_CDK = False
8
+
9
+ try:
10
+ from basilisk import ic # noqa: F401
11
+
12
+ # Verify ic.print actually works (we might be imported but not in IC env)
13
+ try:
14
+ ic.print("")
15
+ IN_IC_ENVIRONMENT = True
16
+ except Exception:
17
+ IN_IC_ENVIRONMENT = False
18
+
19
+ HAS_CDK = True
20
+
21
+ except ImportError:
22
+ ic = None # type: ignore
23
+ IN_IC_ENVIRONMENT = False
24
+
25
+
26
+ def _import_types():
27
+ """Import CDK types for query function definitions.
28
+
29
+ Returns a tuple of (Opt, Record, Vec, nat, query) or None if CDK unavailable.
30
+ """
31
+ try:
32
+ from basilisk import Opt, Record, Vec, nat, query
33
+
34
+ return Opt, Record, Vec, nat, query
35
+ except ImportError:
36
+ return None