logdash 0.1.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.
logdash-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 LogDash
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
+ recursive-include logdash *
4
+ recursive-exclude * __pycache__
5
+ recursive-exclude * *.py[co]
logdash-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,24 @@
1
+ Metadata-Version: 2.4
2
+ Name: logdash
3
+ Version: 0.1.0
4
+ Summary: Python SDK for LogDash logging and metrics service
5
+ Home-page: https://logdash.io
6
+ Author: LogDash
7
+ Author-email: info@logdash.io
8
+ Keywords: logging,metrics,monitoring
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.6
13
+ License-File: LICENSE
14
+ Requires-Dist: requests
15
+ Requires-Dist: colorama
16
+ Dynamic: author
17
+ Dynamic: author-email
18
+ Dynamic: classifier
19
+ Dynamic: home-page
20
+ Dynamic: keywords
21
+ Dynamic: license-file
22
+ Dynamic: requires-dist
23
+ Dynamic: requires-python
24
+ Dynamic: summary
@@ -0,0 +1,60 @@
1
+ # LogDash Python SDK
2
+
3
+ This is the official Python SDK for LogDash - a simple and powerful logging and metrics service.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install logdash
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Basic Usage
14
+
15
+ ```python
16
+ from logdash import create_logdash
17
+
18
+ # Initialize LogDash with your API key
19
+ logdash = create_logdash(params={
20
+ "api_key": "your-api-key-here",
21
+ "host": "https://api.logdash.io", # optional, default value shown
22
+ "verbose": False # optional, default value shown
23
+ })
24
+
25
+ # Access the logger
26
+ logger = logdash["logger"]
27
+ logger.info("This is an info message")
28
+ logger.error("This is an error message")
29
+ logger.debug("This is a debug message")
30
+
31
+ # Log levels supported:
32
+ # - error
33
+ # - warn
34
+ # - info (also available as log())
35
+ # - http
36
+ # - verbose
37
+ # - debug
38
+ # - silly
39
+
40
+ # Access metrics
41
+ metrics = logdash["metrics"]
42
+ metrics.set("page_views", 100) # Set a metric value
43
+ metrics.mutate("user_count", 1) # Change a metric by a value (increment/decrement)
44
+ ```
45
+
46
+ ### Local Development
47
+
48
+ If you don't provide an API key, logs will only be output locally and metrics will not be tracked:
49
+
50
+ ```python
51
+ from logdash import create_logdash
52
+
53
+ # Local only logger
54
+ logdash = create_logdash()
55
+ logdash["logger"].info("This will only be logged locally")
56
+ ```
57
+
58
+ ## License
59
+
60
+ MIT
@@ -0,0 +1,5 @@
1
+ from logdash.logdash import create_logdash
2
+ from logdash.logger.logger import Logger
3
+ from logdash.metrics.metrics import MetricOperation
4
+
5
+ __all__ = ["create_logdash", "Logger", "MetricOperation"]
@@ -0,0 +1,43 @@
1
+ from datetime import datetime
2
+ from typing import Dict, Optional, TypedDict, Any
3
+
4
+ from logdash.logger.logger import Logger
5
+ from logdash.metrics.base_metrics import BaseMetrics
6
+ from logdash.metrics.create_metrics import create_metrics
7
+ from logdash.sync.create_log_sync import create_log_sync
8
+ from logdash.types.initialization_params import InitializationParams, RequiredInitializationParams
9
+ from logdash.types.log_level import LogLevel
10
+
11
+
12
+ class LogdashInstance(TypedDict):
13
+ logger: Logger
14
+ metrics: BaseMetrics
15
+
16
+
17
+ def create_logdash(params: Optional[Dict[str, Any]] = None) -> LogdashInstance:
18
+ init_params = InitializationParams()
19
+
20
+ if params is not None:
21
+ if "api_key" in params:
22
+ init_params.api_key = params["api_key"]
23
+ if "host" in params:
24
+ init_params.host = params["host"]
25
+ if "verbose" in params:
26
+ init_params.verbose = params["verbose"]
27
+
28
+ required_params = RequiredInitializationParams(
29
+ api_key=init_params.api_key or "",
30
+ host=init_params.host or "https://api.logdash.io",
31
+ verbose=init_params.verbose or False,
32
+ )
33
+
34
+ log_sync = create_log_sync(required_params)
35
+ metrics = create_metrics(required_params)
36
+
37
+ def on_log(level: LogLevel, message: str) -> None:
38
+ log_sync.send(message, level, datetime.now().isoformat())
39
+
40
+ return {
41
+ "logger": Logger(log_method=print, on_log=on_log),
42
+ "metrics": metrics,
43
+ }
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,11 @@
1
+ from colorama import Fore, Style
2
+
3
+ from logdash.logger.logger import Logger
4
+ from logdash.types.log_level import LogLevel
5
+
6
+
7
+ def logdash_prefix(level: LogLevel) -> str:
8
+ return f"{Fore.MAGENTA}[LogDash] {Style.RESET_ALL}"
9
+
10
+
11
+ internal_logger = Logger(log_method=print, prefix=logdash_prefix)
@@ -0,0 +1,66 @@
1
+ from datetime import datetime
2
+ from typing import Any, Callable, List, Dict, Optional
3
+
4
+ from colorama import Fore, Style, init
5
+
6
+ from logdash.types.log_level import LogLevel
7
+
8
+ # Initialize colorama
9
+ init()
10
+
11
+ LOG_LEVEL_COLORS: Dict[LogLevel, str] = {
12
+ LogLevel.ERROR: Fore.RED,
13
+ LogLevel.WARN: Fore.YELLOW,
14
+ LogLevel.INFO: Fore.BLUE,
15
+ LogLevel.HTTP: Fore.CYAN,
16
+ LogLevel.VERBOSE: Fore.GREEN,
17
+ LogLevel.DEBUG: Fore.LIGHTGREEN_EX,
18
+ LogLevel.SILLY: Fore.LIGHTBLACK_EX,
19
+ }
20
+
21
+
22
+ class Logger:
23
+ def __init__(
24
+ self,
25
+ log_method: Callable = print,
26
+ prefix: Optional[Callable[[LogLevel], str]] = None,
27
+ on_log: Optional[Callable[[LogLevel, str], None]] = None,
28
+ ):
29
+ self.log_method = log_method
30
+ self.prefix = prefix or (lambda level: f"{level.value.upper()} ")
31
+ self.on_log = on_log
32
+
33
+ def error(self, *data: Any) -> None:
34
+ self._log(LogLevel.ERROR, " ".join(str(item) for item in data))
35
+
36
+ def warn(self, *data: Any) -> None:
37
+ self._log(LogLevel.WARN, " ".join(str(item) for item in data))
38
+
39
+ def info(self, *data: Any) -> None:
40
+ self._log(LogLevel.INFO, " ".join(str(item) for item in data))
41
+
42
+ def log(self, *data: Any) -> None:
43
+ self._log(LogLevel.INFO, " ".join(str(item) for item in data))
44
+
45
+ def http(self, *data: Any) -> None:
46
+ self._log(LogLevel.HTTP, " ".join(str(item) for item in data))
47
+
48
+ def verbose(self, *data: Any) -> None:
49
+ self._log(LogLevel.VERBOSE, " ".join(str(item) for item in data))
50
+
51
+ def debug(self, *data: Any) -> None:
52
+ self._log(LogLevel.DEBUG, " ".join(str(item) for item in data))
53
+
54
+ def silly(self, *data: Any) -> None:
55
+ self._log(LogLevel.SILLY, " ".join(str(item) for item in data))
56
+
57
+ def _log(self, level: LogLevel, message: str) -> None:
58
+ color = LOG_LEVEL_COLORS[level]
59
+
60
+ date_prefix = f"{Fore.LIGHTBLACK_EX}[{datetime.now().isoformat()}]{Style.RESET_ALL}"
61
+ prefix = f"{color}{self.prefix(level)}{Style.RESET_ALL}"
62
+ formatted_message = f"{date_prefix} {prefix}{message}"
63
+
64
+ self.log_method(formatted_message)
65
+ if self.on_log:
66
+ self.on_log(level, message)
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,11 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+
4
+ class BaseMetrics(ABC):
5
+ @abstractmethod
6
+ def set(self, key: str, value: float) -> None:
7
+ pass
8
+
9
+ @abstractmethod
10
+ def mutate(self, key: str, value: float) -> None:
11
+ pass
@@ -0,0 +1,15 @@
1
+ from logdash.logger.internal_logger import internal_logger
2
+ from logdash.metrics.base_metrics import BaseMetrics
3
+ from logdash.metrics.metrics import Metrics
4
+ from logdash.metrics.noop_metrics import NoopMetrics
5
+ from logdash.types.initialization_params import RequiredInitializationParams
6
+
7
+
8
+ def create_metrics(params: RequiredInitializationParams) -> BaseMetrics:
9
+ if not params.api_key:
10
+ internal_logger.log(
11
+ "Api key was not provided in the InitializationParams when calling create_logdash(), metrics will not be registered.\n"
12
+ )
13
+ return NoopMetrics()
14
+
15
+ return Metrics(params)
@@ -0,0 +1,50 @@
1
+ from enum import Enum
2
+ import requests
3
+
4
+ from logdash.logger.internal_logger import internal_logger
5
+ from logdash.metrics.base_metrics import BaseMetrics
6
+ from logdash.types.initialization_params import RequiredInitializationParams
7
+
8
+
9
+ class MetricOperation(str, Enum):
10
+ SET = "set"
11
+ CHANGE = "change"
12
+
13
+
14
+ class Metrics(BaseMetrics):
15
+ def __init__(self, params: RequiredInitializationParams):
16
+ self.params = params
17
+
18
+ def set(self, name: str, value: float) -> None:
19
+ if self.params.verbose:
20
+ internal_logger.verbose(f"Setting metric {name} to {value}")
21
+
22
+ requests.put(
23
+ f"{self.params.host}/metrics",
24
+ headers={
25
+ "Content-Type": "application/json",
26
+ "project-api-key": self.params.api_key,
27
+ },
28
+ json={
29
+ "name": name,
30
+ "value": value,
31
+ "operation": MetricOperation.SET,
32
+ },
33
+ )
34
+
35
+ def mutate(self, name: str, value: float) -> None:
36
+ if self.params.verbose:
37
+ internal_logger.verbose(f"Mutating metric {name} by {value}")
38
+
39
+ requests.put(
40
+ f"{self.params.host}/metrics",
41
+ headers={
42
+ "Content-Type": "application/json",
43
+ "project-api-key": self.params.api_key,
44
+ },
45
+ json={
46
+ "name": name,
47
+ "value": value,
48
+ "operation": MetricOperation.CHANGE,
49
+ },
50
+ )
@@ -0,0 +1,9 @@
1
+ from logdash.metrics.base_metrics import BaseMetrics
2
+
3
+
4
+ class NoopMetrics(BaseMetrics):
5
+ def set(self, key: str, value: float) -> None:
6
+ pass
7
+
8
+ def mutate(self, key: str, value: float) -> None:
9
+ pass
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,15 @@
1
+ from logdash.logger.internal_logger import internal_logger
2
+ from logdash.sync.http_log_sync import HttpLogSync
3
+ from logdash.sync.log_sync import LogSync
4
+ from logdash.sync.noop_log_sync import NoopLogSync
5
+ from logdash.types.initialization_params import RequiredInitializationParams
6
+
7
+
8
+ def create_log_sync(params: RequiredInitializationParams) -> LogSync:
9
+ if not params.api_key:
10
+ internal_logger.log(
11
+ "Api key was not provided in the InitializationParams when calling create_logdash(), using only local logger.\n"
12
+ )
13
+ return NoopLogSync()
14
+
15
+ return HttpLogSync(params)
@@ -0,0 +1,31 @@
1
+ import requests
2
+
3
+ from logdash.sync.log_sync import LogSync
4
+ from logdash.types.initialization_params import RequiredInitializationParams
5
+ from logdash.types.log_level import LogLevel
6
+
7
+
8
+ class HttpLogSync(LogSync):
9
+ def __init__(self, params: RequiredInitializationParams):
10
+ self.params = params
11
+ self.sequence_number = 0
12
+
13
+ # todos:
14
+ # - queue
15
+ # - retry
16
+ # - batching
17
+ def send(self, message: str, level: LogLevel, created_at: str) -> None:
18
+ requests.post(
19
+ f"{self.params.host}/logs",
20
+ headers={
21
+ "Content-Type": "application/json",
22
+ "project-api-key": self.params.api_key,
23
+ },
24
+ json={
25
+ "message": message,
26
+ "level": level,
27
+ "createdAt": created_at,
28
+ "sequenceNumber": self.sequence_number,
29
+ },
30
+ )
31
+ self.sequence_number += 1
@@ -0,0 +1,9 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+ from logdash.types.log_level import LogLevel
4
+
5
+
6
+ class LogSync(ABC):
7
+ @abstractmethod
8
+ def send(self, message: str, level: LogLevel, created_at: str) -> None:
9
+ pass
@@ -0,0 +1,7 @@
1
+ from logdash.sync.log_sync import LogSync
2
+ from logdash.types.log_level import LogLevel
3
+
4
+
5
+ class NoopLogSync(LogSync):
6
+ def send(self, message: str, level: LogLevel, created_at: str) -> None:
7
+ pass
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,16 @@
1
+ from dataclasses import dataclass
2
+ from typing import Optional
3
+
4
+
5
+ @dataclass
6
+ class InitializationParams:
7
+ api_key: Optional[str] = None
8
+ host: Optional[str] = None
9
+ verbose: Optional[bool] = None
10
+
11
+
12
+ @dataclass
13
+ class RequiredInitializationParams:
14
+ api_key: str
15
+ host: str
16
+ verbose: bool
@@ -0,0 +1,11 @@
1
+ from enum import Enum
2
+
3
+
4
+ class LogLevel(str, Enum):
5
+ ERROR = "error"
6
+ WARN = "warning"
7
+ INFO = "info"
8
+ HTTP = "http"
9
+ VERBOSE = "verbose"
10
+ DEBUG = "debug"
11
+ SILLY = "silly"
@@ -0,0 +1,24 @@
1
+ Metadata-Version: 2.4
2
+ Name: logdash
3
+ Version: 0.1.0
4
+ Summary: Python SDK for LogDash logging and metrics service
5
+ Home-page: https://logdash.io
6
+ Author: LogDash
7
+ Author-email: info@logdash.io
8
+ Keywords: logging,metrics,monitoring
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.6
13
+ License-File: LICENSE
14
+ Requires-Dist: requests
15
+ Requires-Dist: colorama
16
+ Dynamic: author
17
+ Dynamic: author-email
18
+ Dynamic: classifier
19
+ Dynamic: home-page
20
+ Dynamic: keywords
21
+ Dynamic: license-file
22
+ Dynamic: requires-dist
23
+ Dynamic: requires-python
24
+ Dynamic: summary
@@ -0,0 +1,27 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.md
4
+ setup.py
5
+ logdash/__init__.py
6
+ logdash/logdash.py
7
+ logdash.egg-info/PKG-INFO
8
+ logdash.egg-info/SOURCES.txt
9
+ logdash.egg-info/dependency_links.txt
10
+ logdash.egg-info/requires.txt
11
+ logdash.egg-info/top_level.txt
12
+ logdash/logger/__init__.py
13
+ logdash/logger/internal_logger.py
14
+ logdash/logger/logger.py
15
+ logdash/metrics/__init__.py
16
+ logdash/metrics/base_metrics.py
17
+ logdash/metrics/create_metrics.py
18
+ logdash/metrics/metrics.py
19
+ logdash/metrics/noop_metrics.py
20
+ logdash/sync/__init__.py
21
+ logdash/sync/create_log_sync.py
22
+ logdash/sync/http_log_sync.py
23
+ logdash/sync/log_sync.py
24
+ logdash/sync/noop_log_sync.py
25
+ logdash/types/__init__.py
26
+ logdash/types/initialization_params.py
27
+ logdash/types/log_level.py
@@ -0,0 +1,2 @@
1
+ requests
2
+ colorama
@@ -0,0 +1 @@
1
+ logdash
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
logdash-0.1.0/setup.py ADDED
@@ -0,0 +1,22 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name="logdash",
5
+ version="0.1.0",
6
+ packages=find_packages(),
7
+ install_requires=[
8
+ "requests",
9
+ "colorama",
10
+ ],
11
+ author="LogDash",
12
+ author_email="info@logdash.io",
13
+ description="Python SDK for LogDash logging and metrics service",
14
+ keywords="logging, metrics, monitoring",
15
+ url="https://logdash.io",
16
+ classifiers=[
17
+ "Programming Language :: Python :: 3",
18
+ "License :: OSI Approved :: MIT License",
19
+ "Operating System :: OS Independent",
20
+ ],
21
+ python_requires=">=3.6",
22
+ )