ipiranga-inovai-project-lib 4.3__py3-none-any.whl → 4.5__py3-none-any.whl

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.
inovai/log.py ADDED
@@ -0,0 +1,119 @@
1
+ import logging
2
+ import logging.config
3
+ from contextvars import ContextVar
4
+ from typing import Optional, List
5
+ import asgi_correlation_id
6
+
7
+
8
+ integration_batch_id: ContextVar[Optional[str]] = ContextVar('integration_batch_id', default=None)
9
+ document_correlation_id: ContextVar[Optional[str]] = ContextVar('document_correlation_id', default=None)
10
+ origin: ContextVar[Optional[str]] = ContextVar('origin', default=None)
11
+ document_type: ContextVar[Optional[str]] = ContextVar('document_type', default=None)
12
+ documents_error: ContextVar[Optional[List[any]]] = ContextVar('documents_error', default=[])
13
+ branch_code: ContextVar[Optional[str]] = ContextVar('branch_code', default=None)
14
+ movement_type: ContextVar[Optional[str]] = ContextVar('movement_type', default=None)
15
+ origin_id: ContextVar[Optional[str]] = ContextVar("origin_id", default=None)
16
+ internal_batch_id: ContextVar[Optional[str]] = ContextVar('internal_batch_id', default=None)
17
+ integration_employer_number: ContextVar[Optional[str]] = ContextVar(
18
+ "integration_employer_number", default=None
19
+ )
20
+ content_type: ContextVar[Optional[str]] = ContextVar("content_type", default=None)
21
+ processing_codes: ContextVar[Optional[list[str]]] = ContextVar('processing_codes', default=None)
22
+ kit_id: ContextVar[Optional[str]] = ContextVar("kit_id", default='4')
23
+
24
+
25
+ class ChunkedStreamHandler(logging.StreamHandler):
26
+ def emit(self, record):
27
+ # Format the record into a log message
28
+ msg = self.format(record)
29
+ msg_size = len(msg.encode('utf-8')) # Get size of the message in bytes
30
+
31
+ max_bytes = 50 * 1024 # 50kb in bytes
32
+
33
+ # If the message is larger than max_bytes, split it into chunks
34
+ if msg_size > max_bytes:
35
+ message = record.msg
36
+ for i in range(0, len(message), max_bytes):
37
+ chunk = message[i:i + max_bytes]
38
+ original_msg = record.msg # Backup original message
39
+ try:
40
+ record.msg = chunk # Alter only the message part
41
+ record.args = None # Reset args to avoid formatting issues
42
+ super().emit(record) # Emit the chunked message
43
+ finally:
44
+ record.msg = original_msg # Restore original message after chunk is emitted
45
+ else:
46
+ # If the message fits, emit it as usual
47
+ super().emit(record)
48
+
49
+
50
+ class CustomLogger(logging.Logger):
51
+
52
+ def _add_extra_args(self, kwargs):
53
+ # Define os `extra_args` a serem adicionados
54
+ extra_args = {
55
+ "integration_batch_id": str(internal_batch_id.get()),
56
+ "correlation_id": str(asgi_correlation_id.correlation_id.get()),
57
+ "document_correlation_id": str(document_correlation_id.get())
58
+ if document_correlation_id.get() is not None else str(asgi_correlation_id.correlation_id.get()).lower(),
59
+ "origin": str(origin.get()),
60
+ "document_type": str(document_type.get()),
61
+ "documents_error": str(documents_error.get()),
62
+ "branch_code": str(branch_code.get()),
63
+ "movement_type": str(movement_type.get()),
64
+ "origin_id": str(origin_id.get()),
65
+ "internal_batch_id": str(internal_batch_id.get()),
66
+ "integration_employer_number": str(integration_employer_number.get()),
67
+ "content_type": str(content_type.get()),
68
+ "processing_codes": str(processing_codes.get()),
69
+ "kit_id": str(kit_id.get())
70
+ }
71
+
72
+ # Atualiza o kwargs do log com `extra_args`
73
+ if "extra" in kwargs:
74
+ kwargs["extra"].update(extra_args)
75
+ else:
76
+ kwargs["extra"] = extra_args
77
+
78
+ return kwargs
79
+
80
+ def info(self, msg, *args, **kwargs):
81
+ # Adicionar os extras para logs do nível INFO
82
+ kwargs = self._add_extra_args(kwargs)
83
+ super().info(msg, *args, **kwargs)
84
+
85
+ def error(self, msg, *args, **kwargs):
86
+ # Adicionar os extras para logs do nível ERROR
87
+ kwargs = self._add_extra_args(kwargs)
88
+ super().error(msg, *args, **kwargs)
89
+
90
+ def warning(self, msg, *args, **kwargs):
91
+ # Adicionar os extras para logs do nível WARNING
92
+ kwargs = self._add_extra_args(kwargs)
93
+ super().warning(msg, *args, **kwargs)
94
+
95
+ def debug(self, msg, *args, **kwargs):
96
+ # Adicionar os extras para logs do nível DEBUG
97
+ kwargs = self._add_extra_args(kwargs)
98
+ super().debug(msg, *args, **kwargs)
99
+
100
+ def critical(self, msg, *args, **kwargs):
101
+ # Adicionar os extras para logs do nível CRITICAL
102
+ kwargs = self._add_extra_args(kwargs)
103
+ super().critical(msg, *args, **kwargs)
104
+
105
+
106
+ def get_logger(name):
107
+ logging.setLoggerClass(CustomLogger)
108
+
109
+ custom_handler = ChunkedStreamHandler()
110
+ custom_handler.setLevel(logging.INFO)
111
+
112
+ log_formatter = logging.Formatter('%(asctime)s.%(msecs)03d %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
113
+ custom_handler.setFormatter(log_formatter)
114
+
115
+ logger = logging.getLogger(name)
116
+ logger.setLevel(logging.INFO)
117
+ logger.addHandler(custom_handler)
118
+
119
+ return logger
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ipiranga-inovai-project-lib
3
- Version: 4.3
3
+ Version: 4.5
4
4
  Summary: Projeto criado para importação genérica de entidades
5
5
  Home-page: https://gitlab.ipirangacloud.com/clayton.monteiro.ext/ipiranga-inovai-project-lib
6
6
  Author: Clayton Sandes Monteiro
@@ -2,6 +2,7 @@ inovai/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  inovai/dtos.py,sha256=qaWZB7n5SHcC58RP351IQ1HLjv7fx3VAdILj-GugVWs,1587
3
3
  inovai/entities.py,sha256=-ANFEKFUI-j020CXo7NQnwI9jgFlbuqR1Takkwiq91E,20766
4
4
  inovai/enums.py,sha256=hj6KZRzV_LZIEnpycCl1yrP2aQdm75pm8WN8CM3TBfI,2367
5
+ inovai/log.py,sha256=ICySFkiOaJnXuXk3ScYNDU2K9g723h5MFDoiafcU8Q4,5201
5
6
  inovai/models/Endereco.py,sha256=Vft20vMwYwTXtEuh5NH7O5FxXVahnLmgaXMDOVdBxY4,3519
6
7
  inovai/models/NFe.py,sha256=MNs9hxUcfUXzrYqUIOEkg-4wNGQQ4gKuduw_gJLHpKk,13241
7
8
  inovai/models/NFeBoleto.py,sha256=tLWqsOlszA6UiJdxesJKQkgwNk_4DuI3NRBCYZh7RKc,5006
@@ -34,8 +35,8 @@ inovai/models/NFeVolume.py,sha256=_cayb-rrz1mSxQ3wFFnB4KCi524jtAsdTTcS4WQTBOc,19
34
35
  inovai/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
35
36
  inovai/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
36
37
  inovai/utils/util.py,sha256=CsxaZmeKWG8A4w_1XzVm_F9hdWkDVgmHDYL2XF8v5dQ,906
37
- ipiranga_inovai_project_lib-4.3.dist-info/LICENSE,sha256=UJ3ql8eZec9Y1kER7QSHAuvdgFPuL47asePHYmdzG_o,198
38
- ipiranga_inovai_project_lib-4.3.dist-info/METADATA,sha256=xdVDnBWH-8dY_YBdnmpWjtJ_oKI4oOZSjK2DbOjUb6Q,518
39
- ipiranga_inovai_project_lib-4.3.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
40
- ipiranga_inovai_project_lib-4.3.dist-info/top_level.txt,sha256=BD-APH5oIEb_KDXwrfnZiu9QDsD9ttTEkYc32r9thqg,7
41
- ipiranga_inovai_project_lib-4.3.dist-info/RECORD,,
38
+ ipiranga_inovai_project_lib-4.5.dist-info/LICENSE,sha256=UJ3ql8eZec9Y1kER7QSHAuvdgFPuL47asePHYmdzG_o,198
39
+ ipiranga_inovai_project_lib-4.5.dist-info/METADATA,sha256=To7vdhOmZbYOitWtWCZTLK8qIUU1CXgy-AK7iJ9xESE,518
40
+ ipiranga_inovai_project_lib-4.5.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
41
+ ipiranga_inovai_project_lib-4.5.dist-info/top_level.txt,sha256=BD-APH5oIEb_KDXwrfnZiu9QDsD9ttTEkYc32r9thqg,7
42
+ ipiranga_inovai_project_lib-4.5.dist-info/RECORD,,