@syswatch/core 1.0.1 → 1.0.3
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.
- package/dist/index.d.mts +109 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +2 -18
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +14 -2
- package/CHANGELOG.md +0 -7
- package/jest.config.ts +0 -9
- package/src/client.spec.ts +0 -164
- package/src/client.ts +0 -93
- package/src/index.ts +0 -2
- package/src/logs.ts +0 -127
- package/src/types.ts +0 -31
- package/tsconfig.json +0 -12
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
declare enum LogLevel {
|
|
2
|
+
DEBUG = "DEBUG",
|
|
3
|
+
INFO = "INFO",
|
|
4
|
+
SUCCESS = "SUCCESS",
|
|
5
|
+
FAILURE = "FAILURE",
|
|
6
|
+
WARN = "WARN",
|
|
7
|
+
ERROR = "ERROR",
|
|
8
|
+
CRITICAL = "CRITICAL"
|
|
9
|
+
}
|
|
10
|
+
interface CreateLogDto {
|
|
11
|
+
type: string;
|
|
12
|
+
level: LogLevel;
|
|
13
|
+
message?: string;
|
|
14
|
+
meta?: Record<string, any>;
|
|
15
|
+
environment?: string;
|
|
16
|
+
userId?: string;
|
|
17
|
+
ip?: string;
|
|
18
|
+
timestamp?: Date | string;
|
|
19
|
+
tags?: string[];
|
|
20
|
+
}
|
|
21
|
+
interface CreateMetricDto {
|
|
22
|
+
type: string;
|
|
23
|
+
value: number;
|
|
24
|
+
meta?: Record<string, any>;
|
|
25
|
+
environment?: string;
|
|
26
|
+
timestamp?: Date | string;
|
|
27
|
+
tags?: string[];
|
|
28
|
+
unit?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type LogOptions = Omit<CreateLogDto, "level" | "message" | "type"> & {
|
|
32
|
+
type?: string;
|
|
33
|
+
};
|
|
34
|
+
declare class Logs {
|
|
35
|
+
private sendLog;
|
|
36
|
+
constructor(sendLog: (log: CreateLogDto) => Promise<void>);
|
|
37
|
+
/**
|
|
38
|
+
* Send a DEBUG log
|
|
39
|
+
* @param message Log message
|
|
40
|
+
* @param options Additional options (meta, tags, etc.)
|
|
41
|
+
*/
|
|
42
|
+
debug(type: string, message: string, options?: LogOptions): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Send an INFO log
|
|
45
|
+
* @param message Log message
|
|
46
|
+
* @param options Additional options (meta, tags, etc.)
|
|
47
|
+
*/
|
|
48
|
+
info(type: string, message: string, options?: LogOptions): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Send a SUCCESS log
|
|
51
|
+
* @param message Log message
|
|
52
|
+
* @param options Additional options (meta, tags, etc.)
|
|
53
|
+
*/
|
|
54
|
+
success(type: string, message: string, options?: LogOptions): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Send a FAILURE log
|
|
57
|
+
* @param message Log message
|
|
58
|
+
* @param options Additional options (meta, tags, etc.)
|
|
59
|
+
*/
|
|
60
|
+
failure(type: string, message: string, options?: LogOptions): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Send a WARN log
|
|
63
|
+
* @param message Log message
|
|
64
|
+
* @param options Additional options (meta, tags, etc.)
|
|
65
|
+
*/
|
|
66
|
+
warn(type: string, message: string, options?: LogOptions): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Send an ERROR log with smart error handling
|
|
69
|
+
* @param message Log message
|
|
70
|
+
* @param errorOrOptions Error object or options object
|
|
71
|
+
*/
|
|
72
|
+
error(type: string, message: string, errorOrOptions?: Error | LogOptions): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Send a CRITICAL log
|
|
75
|
+
* @param message Log message
|
|
76
|
+
* @param options Additional options (meta, tags, etc.)
|
|
77
|
+
*/
|
|
78
|
+
critical(type: string, message: string, options?: LogOptions): Promise<void>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface SysWatchConfig {
|
|
82
|
+
apiKey: string;
|
|
83
|
+
defaultMeta?: Record<string, any>;
|
|
84
|
+
defaultTags?: string[];
|
|
85
|
+
}
|
|
86
|
+
declare class SysWatch {
|
|
87
|
+
private apiKey;
|
|
88
|
+
private baseUrl;
|
|
89
|
+
private defaultMeta;
|
|
90
|
+
private defaultTags;
|
|
91
|
+
log: Logs;
|
|
92
|
+
constructor(config: SysWatchConfig);
|
|
93
|
+
private request;
|
|
94
|
+
/**
|
|
95
|
+
* Send a log entry to SysWatch
|
|
96
|
+
* @param data Log data
|
|
97
|
+
*/
|
|
98
|
+
private sendLog;
|
|
99
|
+
/**
|
|
100
|
+
* Track a metric value
|
|
101
|
+
* @param type Metric type key
|
|
102
|
+
* @param value Metric value
|
|
103
|
+
* @param unit Unit of measurement (optional)
|
|
104
|
+
* @param options Additional options
|
|
105
|
+
*/
|
|
106
|
+
trackMetric(type: string, value: number, unit?: string, options?: Omit<CreateMetricDto, "type" | "value" | "unit">): Promise<any>;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export { type CreateLogDto, type CreateMetricDto, LogLevel, SysWatch, type SysWatchConfig };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
declare enum LogLevel {
|
|
2
|
+
DEBUG = "DEBUG",
|
|
3
|
+
INFO = "INFO",
|
|
4
|
+
SUCCESS = "SUCCESS",
|
|
5
|
+
FAILURE = "FAILURE",
|
|
6
|
+
WARN = "WARN",
|
|
7
|
+
ERROR = "ERROR",
|
|
8
|
+
CRITICAL = "CRITICAL"
|
|
9
|
+
}
|
|
10
|
+
interface CreateLogDto {
|
|
11
|
+
type: string;
|
|
12
|
+
level: LogLevel;
|
|
13
|
+
message?: string;
|
|
14
|
+
meta?: Record<string, any>;
|
|
15
|
+
environment?: string;
|
|
16
|
+
userId?: string;
|
|
17
|
+
ip?: string;
|
|
18
|
+
timestamp?: Date | string;
|
|
19
|
+
tags?: string[];
|
|
20
|
+
}
|
|
21
|
+
interface CreateMetricDto {
|
|
22
|
+
type: string;
|
|
23
|
+
value: number;
|
|
24
|
+
meta?: Record<string, any>;
|
|
25
|
+
environment?: string;
|
|
26
|
+
timestamp?: Date | string;
|
|
27
|
+
tags?: string[];
|
|
28
|
+
unit?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type LogOptions = Omit<CreateLogDto, "level" | "message" | "type"> & {
|
|
32
|
+
type?: string;
|
|
33
|
+
};
|
|
34
|
+
declare class Logs {
|
|
35
|
+
private sendLog;
|
|
36
|
+
constructor(sendLog: (log: CreateLogDto) => Promise<void>);
|
|
37
|
+
/**
|
|
38
|
+
* Send a DEBUG log
|
|
39
|
+
* @param message Log message
|
|
40
|
+
* @param options Additional options (meta, tags, etc.)
|
|
41
|
+
*/
|
|
42
|
+
debug(type: string, message: string, options?: LogOptions): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Send an INFO log
|
|
45
|
+
* @param message Log message
|
|
46
|
+
* @param options Additional options (meta, tags, etc.)
|
|
47
|
+
*/
|
|
48
|
+
info(type: string, message: string, options?: LogOptions): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Send a SUCCESS log
|
|
51
|
+
* @param message Log message
|
|
52
|
+
* @param options Additional options (meta, tags, etc.)
|
|
53
|
+
*/
|
|
54
|
+
success(type: string, message: string, options?: LogOptions): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Send a FAILURE log
|
|
57
|
+
* @param message Log message
|
|
58
|
+
* @param options Additional options (meta, tags, etc.)
|
|
59
|
+
*/
|
|
60
|
+
failure(type: string, message: string, options?: LogOptions): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Send a WARN log
|
|
63
|
+
* @param message Log message
|
|
64
|
+
* @param options Additional options (meta, tags, etc.)
|
|
65
|
+
*/
|
|
66
|
+
warn(type: string, message: string, options?: LogOptions): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Send an ERROR log with smart error handling
|
|
69
|
+
* @param message Log message
|
|
70
|
+
* @param errorOrOptions Error object or options object
|
|
71
|
+
*/
|
|
72
|
+
error(type: string, message: string, errorOrOptions?: Error | LogOptions): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Send a CRITICAL log
|
|
75
|
+
* @param message Log message
|
|
76
|
+
* @param options Additional options (meta, tags, etc.)
|
|
77
|
+
*/
|
|
78
|
+
critical(type: string, message: string, options?: LogOptions): Promise<void>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface SysWatchConfig {
|
|
82
|
+
apiKey: string;
|
|
83
|
+
defaultMeta?: Record<string, any>;
|
|
84
|
+
defaultTags?: string[];
|
|
85
|
+
}
|
|
86
|
+
declare class SysWatch {
|
|
87
|
+
private apiKey;
|
|
88
|
+
private baseUrl;
|
|
89
|
+
private defaultMeta;
|
|
90
|
+
private defaultTags;
|
|
91
|
+
log: Logs;
|
|
92
|
+
constructor(config: SysWatchConfig);
|
|
93
|
+
private request;
|
|
94
|
+
/**
|
|
95
|
+
* Send a log entry to SysWatch
|
|
96
|
+
* @param data Log data
|
|
97
|
+
*/
|
|
98
|
+
private sendLog;
|
|
99
|
+
/**
|
|
100
|
+
* Track a metric value
|
|
101
|
+
* @param type Metric type key
|
|
102
|
+
* @param value Metric value
|
|
103
|
+
* @param unit Unit of measurement (optional)
|
|
104
|
+
* @param options Additional options
|
|
105
|
+
*/
|
|
106
|
+
trackMetric(type: string, value: number, unit?: string, options?: Omit<CreateMetricDto, "type" | "value" | "unit">): Promise<any>;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export { type CreateLogDto, type CreateMetricDto, LogLevel, SysWatch, type SysWatchConfig };
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./types"), exports);
|
|
18
|
-
__exportStar(require("./client"), exports);
|
|
1
|
+
"use strict";var o=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var d=(a,t)=>{for(var s in t)o(a,s,{get:t[s],enumerable:!0})},m=(a,t,s,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of p(t))!h.call(a,r)&&r!==s&&o(a,r,{get:()=>t[r],enumerable:!(e=u(t,r))||e.enumerable});return a};var y=a=>m(o({},"__esModule",{value:!0}),a);var f={};d(f,{LogLevel:()=>l,SysWatch:()=>c});module.exports=y(f);var l=(i=>(i.DEBUG="DEBUG",i.INFO="INFO",i.SUCCESS="SUCCESS",i.FAILURE="FAILURE",i.WARN="WARN",i.ERROR="ERROR",i.CRITICAL="CRITICAL",i))(l||{});var g=class{constructor(t){this.sendLog=t}async debug(t,s,e){return this.sendLog({level:"DEBUG",type:t,message:s,...e})}async info(t,s,e){return this.sendLog({type:t,level:"INFO",message:s,...e})}async success(t,s,e){return this.sendLog({type:t,level:"SUCCESS",message:s,...e})}async failure(t,s,e){return this.sendLog({type:t,level:"FAILURE",message:s,...e})}async warn(t,s,e){return this.sendLog({type:t,level:"WARN",message:s,...e})}async error(t,s,e){let r={},n={};return e instanceof Error?r={error:{name:e.name,message:e.message,stack:e.stack}}:e&&(n=e),this.sendLog({type:t,level:"ERROR",message:s,...n,meta:{...r,...n.meta||{}}})}async critical(t,s,e){return this.sendLog({type:t,level:"CRITICAL",message:s,...e})}};var c=class{constructor(t){this.apiKey=t.apiKey,this.baseUrl="https://syswatchapp.discloud.app/v1",this.defaultMeta=t.defaultMeta||{},this.defaultTags=t.defaultTags||[],this.baseUrl.endsWith("/")&&(this.baseUrl=this.baseUrl.slice(0,-1)),this.log=new g(this.sendLog.bind(this))}async request(t,s){try{let e=await fetch(`${this.baseUrl}${t}`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":this.apiKey,"user-agent":"NodeJS SysWatch SDK "},body:JSON.stringify(s)});if(!e.ok){let r=await e.text();throw new Error(`SysWatch API Error: ${e.status} ${e.statusText} - ${r}`)}return await e.json()}catch(e){throw e}}async sendLog(t){let s={...t,...(this.defaultMeta||t?.meta)&&{meta:{...this.defaultMeta,...t?.meta}},...(this.defaultTags||t?.tags)&&{tags:[...this.defaultTags,...t?.tags||[]]}};return this.request("/logs",s)}async trackMetric(t,s,e,r){let n={type:t,value:s,unit:e,...r,...(this.defaultMeta||r?.meta)&&{meta:{...this.defaultMeta,...r?.meta}},...(this.defaultTags||r?.tags)&&{tags:[...this.defaultTags,...r?.tags||[]]}};return this.request("/metrics",n)}};0&&(module.exports={LogLevel,SysWatch});
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/types.ts","../src/logs.ts","../src/client.ts"],"sourcesContent":["export * from './types';\nexport * from './client';\n","export enum LogLevel {\n DEBUG = 'DEBUG',\n INFO = 'INFO',\n SUCCESS = 'SUCCESS',\n FAILURE = 'FAILURE',\n WARN = 'WARN',\n ERROR = 'ERROR',\n CRITICAL = 'CRITICAL',\n}\n\nexport interface CreateLogDto {\n type: string;\n level: LogLevel;\n message?: string;\n meta?: Record<string, any>;\n environment?: string;\n userId?: string;\n ip?: string;\n timestamp?: Date | string;\n tags?: string[];\n}\n\nexport interface CreateMetricDto {\n type: string;\n value: number;\n meta?: Record<string, any>;\n environment?: string;\n timestamp?: Date | string;\n tags?: string[];\n unit?: string;\n}\n","import { CreateLogDto, LogLevel } from \"./types\";\n\ntype LogOptions = Omit<CreateLogDto, \"level\" | \"message\" | \"type\"> & {\n type?: string;\n};\n\nexport class Logs {\n constructor(private sendLog: (log: CreateLogDto) => Promise<void>) {}\n\n /**\n * Send a DEBUG log\n * @param message Log message\n * @param options Additional options (meta, tags, etc.)\n */\n async debug(type: string, message: string, options?: LogOptions) {\n return this.sendLog({\n level: LogLevel.DEBUG,\n type,\n message,\n ...options,\n });\n }\n\n /**\n * Send an INFO log\n * @param message Log message\n * @param options Additional options (meta, tags, etc.)\n */\n async info(type: string, message: string, options?: LogOptions) {\n return this.sendLog({\n type,\n level: LogLevel.INFO,\n message,\n ...options,\n });\n }\n\n /**\n * Send a SUCCESS log\n * @param message Log message\n * @param options Additional options (meta, tags, etc.)\n */\n async success(type: string, message: string, options?: LogOptions) {\n return this.sendLog({\n type,\n level: LogLevel.SUCCESS,\n message,\n ...options,\n });\n }\n /**\n * Send a FAILURE log\n * @param message Log message\n * @param options Additional options (meta, tags, etc.)\n */\n async failure(type: string, message: string, options?: LogOptions) {\n return this.sendLog({\n type,\n level: LogLevel.FAILURE,\n message,\n ...options,\n });\n }\n\n /**\n * Send a WARN log\n * @param message Log message\n * @param options Additional options (meta, tags, etc.)\n */\n async warn(type: string, message: string, options?: LogOptions) {\n return this.sendLog({\n type,\n level: LogLevel.WARN,\n message,\n ...options,\n });\n }\n\n /**\n * Send an ERROR log with smart error handling\n * @param message Log message\n * @param errorOrOptions Error object or options object\n */\n async error(\n type: string,\n message: string,\n errorOrOptions?: Error | LogOptions,\n ) {\n let meta = {};\n let options: Partial<CreateLogDto> = {};\n\n if (errorOrOptions instanceof Error) {\n meta = {\n error: {\n name: errorOrOptions.name,\n message: errorOrOptions.message,\n stack: errorOrOptions.stack,\n },\n };\n } else if (errorOrOptions) {\n options = errorOrOptions;\n }\n\n return this.sendLog({\n type,\n level: LogLevel.ERROR,\n message,\n ...options,\n meta: { ...meta, ...(options.meta || {}) },\n });\n }\n\n /**\n * Send a CRITICAL log\n * @param message Log message\n * @param options Additional options (meta, tags, etc.)\n */\n async critical(type: string, message: string, options?: LogOptions) {\n return this.sendLog({\n type,\n level: LogLevel.CRITICAL,\n message,\n ...options,\n });\n }\n}\n","import { Logs } from \"./logs\";\nimport { CreateLogDto, CreateMetricDto } from \"./types\";\n\nexport interface SysWatchConfig {\n apiKey: string;\n defaultMeta?: Record<string, any>;\n defaultTags?: string[];\n}\n\nexport class SysWatch {\n private apiKey: string;\n private baseUrl: string;\n private defaultMeta: Record<string, any>;\n private defaultTags: string[];\n\n public log: Logs;\n\n constructor(config: SysWatchConfig) {\n this.apiKey = config.apiKey;\n this.baseUrl = \"https://syswatchapp.discloud.app/v1\";\n this.defaultMeta = config.defaultMeta || {};\n this.defaultTags = config.defaultTags || [];\n\n if (this.baseUrl.endsWith(\"/\")) {\n this.baseUrl = this.baseUrl.slice(0, -1);\n }\n\n this.log = new Logs(this.sendLog.bind(this));\n }\n\n private async request(path: string, body: any) {\n try {\n const response = await fetch(`${this.baseUrl}${path}`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-api-key\": this.apiKey,\n \"user-agent\": \"NodeJS SysWatch SDK \",\n },\n body: JSON.stringify(body),\n });\n\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(\n `SysWatch API Error: ${response.status} ${response.statusText} - ${errorText}`\n );\n }\n\n return await response.json();\n } catch (error) {\n throw error;\n }\n }\n\n /**\n * Send a log entry to SysWatch\n * @param data Log data\n */\n private async sendLog(data: CreateLogDto) {\n const payload: CreateLogDto = {\n ...data,\n ...((this.defaultMeta || data?.meta) && {\n meta: { ...this.defaultMeta, ...data?.meta },\n }),\n ...((this.defaultTags || data?.tags) && {\n tags: [...this.defaultTags, ...(data?.tags || [])],\n }),\n };\n return this.request(\"/logs\", payload);\n }\n\n /**\n * Track a metric value\n * @param type Metric type key\n * @param value Metric value\n * @param unit Unit of measurement (optional)\n * @param options Additional options\n */\n async trackMetric(\n type: string,\n value: number,\n unit?: string,\n options?: Omit<CreateMetricDto, \"type\" | \"value\" | \"unit\">\n ) {\n const payload: CreateMetricDto = {\n type,\n value,\n unit,\n ...options,\n ...((this.defaultMeta || options?.meta) && {\n meta: { ...this.defaultMeta, ...options?.meta },\n }),\n ...((this.defaultTags || options?.tags) && {\n tags: [...this.defaultTags, ...(options?.tags || [])],\n }),\n };\n return this.request(\"/metrics\", payload);\n }\n}\n"],"mappings":"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","names":["index_exports","__export","LogLevel","SysWatch","__toCommonJS","LogLevel","Logs","sendLog","type","message","options","errorOrOptions","meta","SysWatch","config","Logs","path","body","response","errorText","error","data","payload","type","value","unit","options"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var g=(a=>(a.DEBUG="DEBUG",a.INFO="INFO",a.SUCCESS="SUCCESS",a.FAILURE="FAILURE",a.WARN="WARN",a.ERROR="ERROR",a.CRITICAL="CRITICAL",a))(g||{});var n=class{constructor(t){this.sendLog=t}async debug(t,s,e){return this.sendLog({level:"DEBUG",type:t,message:s,...e})}async info(t,s,e){return this.sendLog({type:t,level:"INFO",message:s,...e})}async success(t,s,e){return this.sendLog({type:t,level:"SUCCESS",message:s,...e})}async failure(t,s,e){return this.sendLog({type:t,level:"FAILURE",message:s,...e})}async warn(t,s,e){return this.sendLog({type:t,level:"WARN",message:s,...e})}async error(t,s,e){let r={},i={};return e instanceof Error?r={error:{name:e.name,message:e.message,stack:e.stack}}:e&&(i=e),this.sendLog({type:t,level:"ERROR",message:s,...i,meta:{...r,...i.meta||{}}})}async critical(t,s,e){return this.sendLog({type:t,level:"CRITICAL",message:s,...e})}};var o=class{constructor(t){this.apiKey=t.apiKey,this.baseUrl="https://syswatchapp.discloud.app/v1",this.defaultMeta=t.defaultMeta||{},this.defaultTags=t.defaultTags||[],this.baseUrl.endsWith("/")&&(this.baseUrl=this.baseUrl.slice(0,-1)),this.log=new n(this.sendLog.bind(this))}async request(t,s){try{let e=await fetch(`${this.baseUrl}${t}`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":this.apiKey,"user-agent":"NodeJS SysWatch SDK "},body:JSON.stringify(s)});if(!e.ok){let r=await e.text();throw new Error(`SysWatch API Error: ${e.status} ${e.statusText} - ${r}`)}return await e.json()}catch(e){throw e}}async sendLog(t){let s={...t,...(this.defaultMeta||t?.meta)&&{meta:{...this.defaultMeta,...t?.meta}},...(this.defaultTags||t?.tags)&&{tags:[...this.defaultTags,...t?.tags||[]]}};return this.request("/logs",s)}async trackMetric(t,s,e,r){let i={type:t,value:s,unit:e,...r,...(this.defaultMeta||r?.meta)&&{meta:{...this.defaultMeta,...r?.meta}},...(this.defaultTags||r?.tags)&&{tags:[...this.defaultTags,...r?.tags||[]]}};return this.request("/metrics",i)}};export{g as LogLevel,o as SysWatch};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","../src/logs.ts","../src/client.ts"],"sourcesContent":["export enum LogLevel {\n DEBUG = 'DEBUG',\n INFO = 'INFO',\n SUCCESS = 'SUCCESS',\n FAILURE = 'FAILURE',\n WARN = 'WARN',\n ERROR = 'ERROR',\n CRITICAL = 'CRITICAL',\n}\n\nexport interface CreateLogDto {\n type: string;\n level: LogLevel;\n message?: string;\n meta?: Record<string, any>;\n environment?: string;\n userId?: string;\n ip?: string;\n timestamp?: Date | string;\n tags?: string[];\n}\n\nexport interface CreateMetricDto {\n type: string;\n value: number;\n meta?: Record<string, any>;\n environment?: string;\n timestamp?: Date | string;\n tags?: string[];\n unit?: string;\n}\n","import { CreateLogDto, LogLevel } from \"./types\";\n\ntype LogOptions = Omit<CreateLogDto, \"level\" | \"message\" | \"type\"> & {\n type?: string;\n};\n\nexport class Logs {\n constructor(private sendLog: (log: CreateLogDto) => Promise<void>) {}\n\n /**\n * Send a DEBUG log\n * @param message Log message\n * @param options Additional options (meta, tags, etc.)\n */\n async debug(type: string, message: string, options?: LogOptions) {\n return this.sendLog({\n level: LogLevel.DEBUG,\n type,\n message,\n ...options,\n });\n }\n\n /**\n * Send an INFO log\n * @param message Log message\n * @param options Additional options (meta, tags, etc.)\n */\n async info(type: string, message: string, options?: LogOptions) {\n return this.sendLog({\n type,\n level: LogLevel.INFO,\n message,\n ...options,\n });\n }\n\n /**\n * Send a SUCCESS log\n * @param message Log message\n * @param options Additional options (meta, tags, etc.)\n */\n async success(type: string, message: string, options?: LogOptions) {\n return this.sendLog({\n type,\n level: LogLevel.SUCCESS,\n message,\n ...options,\n });\n }\n /**\n * Send a FAILURE log\n * @param message Log message\n * @param options Additional options (meta, tags, etc.)\n */\n async failure(type: string, message: string, options?: LogOptions) {\n return this.sendLog({\n type,\n level: LogLevel.FAILURE,\n message,\n ...options,\n });\n }\n\n /**\n * Send a WARN log\n * @param message Log message\n * @param options Additional options (meta, tags, etc.)\n */\n async warn(type: string, message: string, options?: LogOptions) {\n return this.sendLog({\n type,\n level: LogLevel.WARN,\n message,\n ...options,\n });\n }\n\n /**\n * Send an ERROR log with smart error handling\n * @param message Log message\n * @param errorOrOptions Error object or options object\n */\n async error(\n type: string,\n message: string,\n errorOrOptions?: Error | LogOptions,\n ) {\n let meta = {};\n let options: Partial<CreateLogDto> = {};\n\n if (errorOrOptions instanceof Error) {\n meta = {\n error: {\n name: errorOrOptions.name,\n message: errorOrOptions.message,\n stack: errorOrOptions.stack,\n },\n };\n } else if (errorOrOptions) {\n options = errorOrOptions;\n }\n\n return this.sendLog({\n type,\n level: LogLevel.ERROR,\n message,\n ...options,\n meta: { ...meta, ...(options.meta || {}) },\n });\n }\n\n /**\n * Send a CRITICAL log\n * @param message Log message\n * @param options Additional options (meta, tags, etc.)\n */\n async critical(type: string, message: string, options?: LogOptions) {\n return this.sendLog({\n type,\n level: LogLevel.CRITICAL,\n message,\n ...options,\n });\n }\n}\n","import { Logs } from \"./logs\";\nimport { CreateLogDto, CreateMetricDto } from \"./types\";\n\nexport interface SysWatchConfig {\n apiKey: string;\n defaultMeta?: Record<string, any>;\n defaultTags?: string[];\n}\n\nexport class SysWatch {\n private apiKey: string;\n private baseUrl: string;\n private defaultMeta: Record<string, any>;\n private defaultTags: string[];\n\n public log: Logs;\n\n constructor(config: SysWatchConfig) {\n this.apiKey = config.apiKey;\n this.baseUrl = \"https://syswatchapp.discloud.app/v1\";\n this.defaultMeta = config.defaultMeta || {};\n this.defaultTags = config.defaultTags || [];\n\n if (this.baseUrl.endsWith(\"/\")) {\n this.baseUrl = this.baseUrl.slice(0, -1);\n }\n\n this.log = new Logs(this.sendLog.bind(this));\n }\n\n private async request(path: string, body: any) {\n try {\n const response = await fetch(`${this.baseUrl}${path}`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-api-key\": this.apiKey,\n \"user-agent\": \"NodeJS SysWatch SDK \",\n },\n body: JSON.stringify(body),\n });\n\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(\n `SysWatch API Error: ${response.status} ${response.statusText} - ${errorText}`\n );\n }\n\n return await response.json();\n } catch (error) {\n throw error;\n }\n }\n\n /**\n * Send a log entry to SysWatch\n * @param data Log data\n */\n private async sendLog(data: CreateLogDto) {\n const payload: CreateLogDto = {\n ...data,\n ...((this.defaultMeta || data?.meta) && {\n meta: { ...this.defaultMeta, ...data?.meta },\n }),\n ...((this.defaultTags || data?.tags) && {\n tags: [...this.defaultTags, ...(data?.tags || [])],\n }),\n };\n return this.request(\"/logs\", payload);\n }\n\n /**\n * Track a metric value\n * @param type Metric type key\n * @param value Metric value\n * @param unit Unit of measurement (optional)\n * @param options Additional options\n */\n async trackMetric(\n type: string,\n value: number,\n unit?: string,\n options?: Omit<CreateMetricDto, \"type\" | \"value\" | \"unit\">\n ) {\n const payload: CreateMetricDto = {\n type,\n value,\n unit,\n ...options,\n ...((this.defaultMeta || options?.meta) && {\n meta: { ...this.defaultMeta, ...options?.meta },\n }),\n ...((this.defaultTags || options?.tags) && {\n tags: [...this.defaultTags, ...(options?.tags || [])],\n }),\n };\n return this.request(\"/metrics\", payload);\n }\n}\n"],"mappings":"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","names":["LogLevel","Logs","sendLog","type","message","options","errorOrOptions","meta","SysWatch","config","Logs","path","body","response","errorText","error","data","payload","type","value","unit","options"]}
|
package/package.json
CHANGED
|
@@ -1,19 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syswatch/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
|
+
"module": "dist/index.mjs",
|
|
5
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"require": "./dist/index.js",
|
|
11
|
+
"import": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
6
17
|
"publishConfig": {
|
|
7
18
|
"access": "public"
|
|
8
19
|
},
|
|
9
20
|
"scripts": {
|
|
10
|
-
"build": "
|
|
21
|
+
"build": "tsup",
|
|
11
22
|
"test": "jest"
|
|
12
23
|
},
|
|
13
24
|
"devDependencies": {
|
|
14
25
|
"@types/jest": "^30.0.0",
|
|
15
26
|
"jest": "^30.2.0",
|
|
16
27
|
"ts-jest": "^29.4.6",
|
|
28
|
+
"tsup": "^8.5.1",
|
|
17
29
|
"typescript": "^5.0.0"
|
|
18
30
|
}
|
|
19
31
|
}
|
package/CHANGELOG.md
DELETED
package/jest.config.ts
DELETED
package/src/client.spec.ts
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import { SysWatch } from "./client";
|
|
2
|
-
import { Logs } from "./logs";
|
|
3
|
-
|
|
4
|
-
// Mock global fetch
|
|
5
|
-
const mockFetch = jest.fn();
|
|
6
|
-
global.fetch = mockFetch;
|
|
7
|
-
|
|
8
|
-
describe("SysWatch", () => {
|
|
9
|
-
const apiKey = "test-api-key";
|
|
10
|
-
const defaultBaseUrl = "https://syswatchapp.discloud.app/v1";
|
|
11
|
-
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
mockFetch.mockClear();
|
|
14
|
-
mockFetch.mockResolvedValue({
|
|
15
|
-
ok: true,
|
|
16
|
-
json: async () => ({ success: true }),
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe("Constructor", () => {
|
|
21
|
-
it("should initialize with default base URL", () => {
|
|
22
|
-
const client = new SysWatch({ apiKey });
|
|
23
|
-
expect(client["baseUrl"]).toBe(defaultBaseUrl);
|
|
24
|
-
expect(client.log).toBeInstanceOf(Logs);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("should store default meta and tags", () => {
|
|
28
|
-
const defaultMeta = { app: "test" };
|
|
29
|
-
const defaultTags = ["test-env"];
|
|
30
|
-
const client = new SysWatch({
|
|
31
|
-
apiKey,
|
|
32
|
-
defaultMeta,
|
|
33
|
-
defaultTags,
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
expect(client["defaultMeta"]).toEqual(defaultMeta);
|
|
37
|
-
expect(client["defaultTags"]).toEqual(defaultTags);
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
describe("sendLog (via log property)", () => {
|
|
42
|
-
let client: SysWatch;
|
|
43
|
-
|
|
44
|
-
beforeEach(() => {
|
|
45
|
-
client = new SysWatch({ apiKey });
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it("should send info log", async () => {
|
|
49
|
-
await client.log.info("Info message", { meta: { foo: "bar" } });
|
|
50
|
-
|
|
51
|
-
expect(mockFetch).toHaveBeenCalledWith(
|
|
52
|
-
`${defaultBaseUrl}/logs`,
|
|
53
|
-
expect.objectContaining({
|
|
54
|
-
method: "POST",
|
|
55
|
-
headers: {
|
|
56
|
-
"Content-Type": "application/json",
|
|
57
|
-
"x-api-key": apiKey,
|
|
58
|
-
"user-agent": "NodeJS SysWatch SDK ",
|
|
59
|
-
},
|
|
60
|
-
body: expect.stringContaining('"level":"INFO"'),
|
|
61
|
-
})
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
const call = mockFetch.mock.calls[0];
|
|
65
|
-
const body = JSON.parse(call[1].body);
|
|
66
|
-
expect(body.type).toBe("info");
|
|
67
|
-
expect(body.message).toBe("Info message");
|
|
68
|
-
expect(body.meta).toEqual({ foo: "bar" });
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it("should send error log from Error object", async () => {
|
|
72
|
-
const error = new Error("Something went wrong");
|
|
73
|
-
error.stack = "Error stack trace";
|
|
74
|
-
|
|
75
|
-
await client.log.error("Failure", error);
|
|
76
|
-
|
|
77
|
-
const call = mockFetch.mock.calls[0];
|
|
78
|
-
const body = JSON.parse(call[1].body);
|
|
79
|
-
expect(body.level).toBe("ERROR");
|
|
80
|
-
expect(body.meta.error).toEqual({
|
|
81
|
-
name: "Error",
|
|
82
|
-
message: "Something went wrong",
|
|
83
|
-
stack: "Error stack trace",
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it("should send error log from options object", async () => {
|
|
88
|
-
await client.log.error("Failure", { meta: { code: 500 } });
|
|
89
|
-
|
|
90
|
-
const call = mockFetch.mock.calls[0];
|
|
91
|
-
const body = JSON.parse(call[1].body);
|
|
92
|
-
expect(body.meta.code).toBe(500);
|
|
93
|
-
expect(body.level).toBe("ERROR");
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
describe("trackMetric", () => {
|
|
98
|
-
let client: SysWatch;
|
|
99
|
-
|
|
100
|
-
beforeEach(() => {
|
|
101
|
-
client = new SysWatch({ apiKey });
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it("should send metric request correctly", async () => {
|
|
105
|
-
await client.trackMetric("cpu_usage", 80, "%");
|
|
106
|
-
|
|
107
|
-
expect(mockFetch).toHaveBeenCalledWith(`${defaultBaseUrl}/metrics`, {
|
|
108
|
-
method: "POST",
|
|
109
|
-
headers: {
|
|
110
|
-
"Content-Type": "application/json",
|
|
111
|
-
"x-api-key": apiKey,
|
|
112
|
-
"user-agent": "NodeJS SysWatch SDK ",
|
|
113
|
-
},
|
|
114
|
-
body: expect.any(String),
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
const call = mockFetch.mock.calls[0];
|
|
118
|
-
const body = JSON.parse(call[1].body);
|
|
119
|
-
expect(body).toEqual({
|
|
120
|
-
type: "cpu_usage",
|
|
121
|
-
value: 80,
|
|
122
|
-
unit: "%",
|
|
123
|
-
meta: {},
|
|
124
|
-
tags: [],
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it("should merge default meta and tags for metrics", async () => {
|
|
129
|
-
client = new SysWatch({
|
|
130
|
-
apiKey,
|
|
131
|
-
defaultMeta: { host: "server-1" },
|
|
132
|
-
defaultTags: ["prod"],
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
await client.trackMetric("memory", 1024, "MB", { tags: ["worker"] });
|
|
136
|
-
|
|
137
|
-
const call = mockFetch.mock.calls[0];
|
|
138
|
-
const body = JSON.parse(call[1].body);
|
|
139
|
-
expect(body.meta).toEqual({ host: "server-1" });
|
|
140
|
-
expect(body.tags).toEqual(["prod", "worker"]);
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
describe("Error Handling", () => {
|
|
145
|
-
let client: SysWatch;
|
|
146
|
-
|
|
147
|
-
beforeEach(() => {
|
|
148
|
-
client = new SysWatch({ apiKey });
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
it("should throw error when API returns non-200", async () => {
|
|
152
|
-
mockFetch.mockResolvedValue({
|
|
153
|
-
ok: false,
|
|
154
|
-
status: 401,
|
|
155
|
-
statusText: "Unauthorized",
|
|
156
|
-
text: async () => "Invalid API Key",
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
await expect(client.log.info("test")).rejects.toThrow(
|
|
160
|
-
"SysWatch API Error: 401 Unauthorized - Invalid API Key"
|
|
161
|
-
);
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
});
|
package/src/client.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { Logs } from "./logs";
|
|
2
|
-
import { CreateLogDto, CreateMetricDto } from "./types";
|
|
3
|
-
|
|
4
|
-
export interface SysWatchConfig {
|
|
5
|
-
apiKey: string;
|
|
6
|
-
defaultMeta?: Record<string, any>;
|
|
7
|
-
defaultTags?: string[];
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export class SysWatch {
|
|
11
|
-
private apiKey: string;
|
|
12
|
-
private baseUrl: string;
|
|
13
|
-
private defaultMeta: Record<string, any>;
|
|
14
|
-
private defaultTags: string[];
|
|
15
|
-
|
|
16
|
-
public log: Logs;
|
|
17
|
-
|
|
18
|
-
constructor(config: SysWatchConfig) {
|
|
19
|
-
this.apiKey = config.apiKey;
|
|
20
|
-
this.baseUrl = "https://syswatchapp.discloud.app/v1";
|
|
21
|
-
this.defaultMeta = config.defaultMeta || {};
|
|
22
|
-
this.defaultTags = config.defaultTags || [];
|
|
23
|
-
|
|
24
|
-
if (this.baseUrl.endsWith("/")) {
|
|
25
|
-
this.baseUrl = this.baseUrl.slice(0, -1);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
this.log = new Logs(this.sendLog.bind(this));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
private async request(path: string, body: any) {
|
|
32
|
-
try {
|
|
33
|
-
const response = await fetch(`${this.baseUrl}${path}`, {
|
|
34
|
-
method: "POST",
|
|
35
|
-
headers: {
|
|
36
|
-
"Content-Type": "application/json",
|
|
37
|
-
"x-api-key": this.apiKey,
|
|
38
|
-
"user-agent": "NodeJS SysWatch SDK ",
|
|
39
|
-
},
|
|
40
|
-
body: JSON.stringify(body),
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
if (!response.ok) {
|
|
44
|
-
const errorText = await response.text();
|
|
45
|
-
throw new Error(
|
|
46
|
-
`SysWatch API Error: ${response.status} ${response.statusText} - ${errorText}`
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return await response.json();
|
|
51
|
-
} catch (error) {
|
|
52
|
-
// Re-throw the error to let the caller handle it
|
|
53
|
-
throw error;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Send a log entry to SysWatch
|
|
59
|
-
* @param data Log data
|
|
60
|
-
*/
|
|
61
|
-
async sendLog(data: CreateLogDto) {
|
|
62
|
-
const payload: CreateLogDto = {
|
|
63
|
-
...data,
|
|
64
|
-
meta: { ...this.defaultMeta, ...data.meta },
|
|
65
|
-
tags: [...this.defaultTags, ...(data.tags || [])],
|
|
66
|
-
};
|
|
67
|
-
return this.request("/logs", payload);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Track a metric value
|
|
72
|
-
* @param type Metric type key
|
|
73
|
-
* @param value Metric value
|
|
74
|
-
* @param unit Unit of measurement (optional)
|
|
75
|
-
* @param options Additional options
|
|
76
|
-
*/
|
|
77
|
-
async trackMetric(
|
|
78
|
-
type: string,
|
|
79
|
-
value: number,
|
|
80
|
-
unit?: string,
|
|
81
|
-
options?: Omit<CreateMetricDto, "type" | "value" | "unit">
|
|
82
|
-
) {
|
|
83
|
-
const payload: CreateMetricDto = {
|
|
84
|
-
type,
|
|
85
|
-
value,
|
|
86
|
-
unit,
|
|
87
|
-
...options,
|
|
88
|
-
meta: { ...this.defaultMeta, ...options?.meta },
|
|
89
|
-
tags: [...this.defaultTags, ...(options?.tags || [])],
|
|
90
|
-
};
|
|
91
|
-
return this.request("/metrics", payload);
|
|
92
|
-
}
|
|
93
|
-
}
|
package/src/index.ts
DELETED
package/src/logs.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { CreateLogDto, LogLevel } from "./types";
|
|
2
|
-
|
|
3
|
-
type LogOptions = Omit<CreateLogDto, "level" | "message" | "type"> & {
|
|
4
|
-
type?: string;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
export class Logs {
|
|
8
|
-
constructor(private sendLog: (log: CreateLogDto) => Promise<void>) {}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Send a DEBUG log
|
|
12
|
-
* @param message Log message
|
|
13
|
-
* @param options Additional options (meta, tags, etc.)
|
|
14
|
-
*/
|
|
15
|
-
async debug(message: string, options?: LogOptions) {
|
|
16
|
-
return this.sendLog({
|
|
17
|
-
type: "debug",
|
|
18
|
-
level: LogLevel.DEBUG,
|
|
19
|
-
message,
|
|
20
|
-
...options,
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Send an INFO log
|
|
26
|
-
* @param message Log message
|
|
27
|
-
* @param options Additional options (meta, tags, etc.)
|
|
28
|
-
*/
|
|
29
|
-
async info(message: string, options?: LogOptions) {
|
|
30
|
-
return this.sendLog({
|
|
31
|
-
type: "info",
|
|
32
|
-
level: LogLevel.INFO,
|
|
33
|
-
message,
|
|
34
|
-
...options,
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Send a SUCCESS log
|
|
40
|
-
* @param message Log message
|
|
41
|
-
* @param options Additional options (meta, tags, etc.)
|
|
42
|
-
*/
|
|
43
|
-
async success(message: string, options?: LogOptions) {
|
|
44
|
-
return this.sendLog({
|
|
45
|
-
type: "success",
|
|
46
|
-
level: LogLevel.SUCCESS,
|
|
47
|
-
message,
|
|
48
|
-
...options,
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Send a FAILURE log
|
|
53
|
-
* @param message Log message
|
|
54
|
-
* @param options Additional options (meta, tags, etc.)
|
|
55
|
-
*/
|
|
56
|
-
async failure(message: string, options?: LogOptions) {
|
|
57
|
-
return this.sendLog({
|
|
58
|
-
type: "failure",
|
|
59
|
-
level: LogLevel.FAILURE,
|
|
60
|
-
message,
|
|
61
|
-
...options,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Send a WARN log
|
|
67
|
-
* @param message Log message
|
|
68
|
-
* @param options Additional options (meta, tags, etc.)
|
|
69
|
-
*/
|
|
70
|
-
async warn(message: string, options?: LogOptions) {
|
|
71
|
-
return this.sendLog({
|
|
72
|
-
type: "warn",
|
|
73
|
-
level: LogLevel.WARN,
|
|
74
|
-
message,
|
|
75
|
-
...options,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Send an ERROR log with smart error handling
|
|
81
|
-
* @param message Log message
|
|
82
|
-
* @param errorOrOptions Error object or options object
|
|
83
|
-
*/
|
|
84
|
-
async error(
|
|
85
|
-
message: string,
|
|
86
|
-
errorOrOptions?:
|
|
87
|
-
| Error
|
|
88
|
-
| (Omit<CreateLogDto, "level" | "message" | "type"> & { type?: string })
|
|
89
|
-
) {
|
|
90
|
-
let meta = {};
|
|
91
|
-
let options: Partial<CreateLogDto> = {};
|
|
92
|
-
|
|
93
|
-
if (errorOrOptions instanceof Error) {
|
|
94
|
-
meta = {
|
|
95
|
-
error: {
|
|
96
|
-
name: errorOrOptions.name,
|
|
97
|
-
message: errorOrOptions.message,
|
|
98
|
-
stack: errorOrOptions.stack,
|
|
99
|
-
},
|
|
100
|
-
};
|
|
101
|
-
} else if (errorOrOptions) {
|
|
102
|
-
options = errorOrOptions;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return this.sendLog({
|
|
106
|
-
type: "error",
|
|
107
|
-
level: LogLevel.ERROR,
|
|
108
|
-
message,
|
|
109
|
-
...options,
|
|
110
|
-
meta: { ...meta, ...(options.meta || {}) },
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Send a CRITICAL log
|
|
116
|
-
* @param message Log message
|
|
117
|
-
* @param options Additional options (meta, tags, etc.)
|
|
118
|
-
*/
|
|
119
|
-
async critical(message: string, options?: LogOptions) {
|
|
120
|
-
return this.sendLog({
|
|
121
|
-
type: "critical",
|
|
122
|
-
level: LogLevel.CRITICAL,
|
|
123
|
-
message,
|
|
124
|
-
...options,
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
export enum LogLevel {
|
|
2
|
-
DEBUG = 'DEBUG',
|
|
3
|
-
INFO = 'INFO',
|
|
4
|
-
SUCCESS = 'SUCCESS',
|
|
5
|
-
FAILURE = 'FAILURE',
|
|
6
|
-
WARN = 'WARN',
|
|
7
|
-
ERROR = 'ERROR',
|
|
8
|
-
CRITICAL = 'CRITICAL',
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface CreateLogDto {
|
|
12
|
-
type: string;
|
|
13
|
-
level: LogLevel;
|
|
14
|
-
message?: string;
|
|
15
|
-
meta?: Record<string, any>;
|
|
16
|
-
environment?: string;
|
|
17
|
-
userId?: string;
|
|
18
|
-
ip?: string;
|
|
19
|
-
timestamp?: Date | string;
|
|
20
|
-
tags?: string[];
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface CreateMetricDto {
|
|
24
|
-
type: string;
|
|
25
|
-
value: number;
|
|
26
|
-
meta?: Record<string, any>;
|
|
27
|
-
environment?: string;
|
|
28
|
-
timestamp?: Date | string;
|
|
29
|
-
tags?: string[];
|
|
30
|
-
unit?: string;
|
|
31
|
-
}
|