Comparing version 0.1.0 to 0.1.1
@@ -0,21 +1,11 @@ | ||
/** | ||
* @fileoverview This module provides a logging utility for Elysia, a Node.js framework. | ||
* It allows for customizable logging of HTTP requests and responses. | ||
*/ | ||
import Elysia from 'elysia'; | ||
export type Attribute = { | ||
ip?: string; | ||
method?: string; | ||
path?: string; | ||
body?: any; | ||
query?: Record<string, string | undefined>; | ||
time?: Date; | ||
contentLength?: number; | ||
status?: any; | ||
referer?: string; | ||
userAgent?: string; | ||
}; | ||
type PresetValue = { | ||
uses: (keyof Attribute)[]; | ||
format: (attr: Attribute) => string; | ||
}; | ||
type Presets = { | ||
common: PresetValue; | ||
}; | ||
import { Attribute, Presets } from './types'; | ||
export type { Attribute }; | ||
/** | ||
* Logestic class provides methods to configure and perform logging. | ||
*/ | ||
export declare class Logestic { | ||
@@ -25,9 +15,32 @@ private requestedAttrs; | ||
private static defaultLogger; | ||
/** | ||
* Constructs a new Logestic instance. | ||
* @param logger - A custom logger function. Defaults to the console logger. | ||
*/ | ||
constructor(logger?: typeof Logestic.defaultLogger); | ||
/** | ||
* Requests Logestic to provide a particular attribute. | ||
* @param attrs - An attribute key or an array of attribute keys. | ||
* @returns The Logestic instance for chaining. | ||
*/ | ||
use(attr: keyof Attribute): Logestic; | ||
use(attrs: (keyof Attribute)[]): Logestic; | ||
/** | ||
* Creates a new Elysia instance with a preset logging configuration. | ||
* @param name - The name of the preset to use. | ||
* @param logger - A custom logger function. Defaults to the console logger. | ||
* @returns A new Elysia instance. | ||
*/ | ||
static preset(name: keyof Presets, logger?: typeof Logestic.defaultLogger): Elysia; | ||
/** | ||
* Configures a custom logging format and attaches it to the Elysia instance. | ||
* @param format - A function that takes an Attribute object and returns a string. | ||
* @returns A new Elysia instance. | ||
*/ | ||
custom(format: (attr: Attribute) => string): Elysia; | ||
/** | ||
* Logs a message using the configured logger function. | ||
* @param msg - The message to log. | ||
*/ | ||
log(msg: string): void; | ||
} | ||
export {}; |
// @bun | ||
import H from 'elysia'; | ||
var I = { | ||
common: { | ||
uses: ['ip', 'method', 'path', 'status', 'contentLength'], | ||
format: ({ ip: P, method: k, path: j, status: w, contentLength: B }) => { | ||
return `${P} ${k} ${j} ${w} ${B}`; | ||
} | ||
} | ||
}, | ||
J = (P, k) => { | ||
const { request: j, path: w, body: B, query: C, set: F } = P; | ||
let v = {}; | ||
for (let G in k) | ||
switch (G) { | ||
case 'ip': | ||
v.ip = j.headers.get('x-forwarded-for') || '<ip?>'; | ||
break; | ||
case 'method': | ||
v.method = j.method; | ||
break; | ||
case 'path': | ||
v.path = w; | ||
break; | ||
case 'body': | ||
v.body = B; | ||
break; | ||
case 'query': | ||
v.query = C; | ||
break; | ||
case 'time': | ||
v.time = new Date(); | ||
break; | ||
case 'contentLength': | ||
v.contentLength = Number(j.headers.get('content-length')); | ||
break; | ||
case 'status': | ||
v.status = F.status; | ||
break; | ||
case 'referer': | ||
v.referer = j.headers.get('referer') || '<referer?>'; | ||
break; | ||
case 'userAgent': | ||
v.userAgent = j.headers.get('user-agent') || '<user-agent?>'; | ||
break; | ||
} | ||
return v; | ||
}; | ||
class z { | ||
requestedAttrs; | ||
logger; | ||
static defaultLogger = P => { | ||
console.log(P); | ||
}; | ||
constructor(P = z.defaultLogger) { | ||
(this.requestedAttrs = {}), (this.logger = P); | ||
} | ||
use(P) { | ||
if (Array.isArray(P)) { | ||
for (let k of P) this.requestedAttrs[k] = !0; | ||
return this; | ||
} | ||
return (this.requestedAttrs[P] = !0), this; | ||
} | ||
static preset(P, k = z.defaultLogger) { | ||
const { uses: j, format: w } = I[P]; | ||
return new z(k).use(j).custom(w); | ||
} | ||
custom(P) { | ||
return new H() | ||
.onAfterHandle({ as: 'global' }, k => { | ||
let j = J(k, this.requestedAttrs); | ||
const w = P(j); | ||
this.log(w); | ||
}) | ||
.onError({ as: 'global' }, ({ request: k, error: j }) => { | ||
this.log(`Error: ${k.method} ${k.url} ${j.message}`); | ||
}); | ||
} | ||
log(P) { | ||
this.logger(P); | ||
} | ||
} | ||
export { z as Logestic }; | ||
import K from"elysia";var I={uses:["ip","method","path","status","contentLength"],format:({ip:d,method:v,path:j,status:z,contentLength:C})=>{return`${d} ${v} ${j} ${z} ${C}`}},D=I;var J={common:D},E=J;var M=(d,v)=>{const{request:j,path:z,body:C,query:F,set:G}=d;let w={};for(let H in v)switch(H){case"ip":w.ip=j.headers.get("x-forwarded-for")||"<ip?>";break;case"method":w.method=j.method;break;case"path":w.path=z;break;case"body":w.body=C;break;case"query":w.query=F;break;case"time":w.time=new Date;break;case"contentLength":w.contentLength=Number(j.headers.get("content-length"));break;case"status":w.status=G.status;break;case"referer":w.referer=j.headers.get("referer")||"<referer?>";break;case"userAgent":w.userAgent=j.headers.get("user-agent")||"<user-agent?>";break}return w};class B{requestedAttrs;logger;static defaultLogger=(d)=>{console.log(d)};constructor(d=B.defaultLogger){this.requestedAttrs={},this.logger=d}use(d){if(Array.isArray(d)){for(let v of d)this.requestedAttrs[v]=!0;return this}return this.requestedAttrs[d]=!0,this}static preset(d,v=B.defaultLogger){const{uses:j,format:z}=E[d];return new B(v).use(j).custom(z)}custom(d){return new K().onAfterHandle({as:"global"},(v)=>{let j=M(v,this.requestedAttrs);const z=d(j);this.log(z)}).onError({as:"global"},({request:v,error:j})=>{this.log(`Error: ${v.method} ${v.url} ${j.message}`)})}log(d){this.logger(d)}}export{B as Logestic}; |
{ | ||
"name": "logestic", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"author": "Nishant Aanjaney Jalan <cybercoder.nishant@gmail.com>", | ||
@@ -27,3 +27,3 @@ "description": "An advanced and customisable logging library for ElysiaJS", | ||
"scripts": { | ||
"build": "rimraf dist && tsc", | ||
"build": "rimraf dist && bun build src/index.ts --outdir dist --target bun --minify -e elysia && tsc", | ||
"test": "bun test" | ||
@@ -30,0 +30,0 @@ }, |
# Logestic | ||
To install dependencies: | ||
An advanced and customisable logging library for ElysiaJS | ||
## Table of Contents | ||
- [Logestic](#logestic) | ||
- [Table of Contents](#table-of-contents) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Preset request logging](#preset-request-logging) | ||
- [Custom request logging](#custom-request-logging) | ||
- [Contributing Guidelines](#contributing-guidelines) | ||
- [License](#license) | ||
- [Authors](#authors) | ||
## Installation | ||
Add the package with your favourite package manager to your Elysia Project. | ||
```bash | ||
bun install | ||
npm install --save logestic | ||
# or | ||
yarn add logestic | ||
# or | ||
pnpm add logestic | ||
# or | ||
bun add logestic | ||
``` | ||
**Note**: You must have `elysia@1.0` installed in your project. | ||
To run: | ||
## Usage | ||
```bash | ||
bun run src/index.ts | ||
There are two ways to add logging to your Elysia application. | ||
### Preset request logging | ||
Currently there are these [presets](./src/presets/index.ts) availble to use. | ||
```typescript | ||
import { Elysia } from 'elysia'; | ||
import { Logestic } from 'logestic'; | ||
const app = new Elysia() | ||
.use(Logestic.preset('common')) | ||
.get('/', () => "Hello from server") | ||
.listen(5566); | ||
``` | ||
This project was created using `bun init` in bun v1.0.30. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. | ||
### Custom request logging | ||
If you don't like any of presets, you can configure Logestic to log your requests in your way. | ||
1. Create a `Logestic` instance, optionally where you wish to log. | ||
2. Call `use` to tell `Logestic` the information you wish to use. | ||
3. Finally, create an `Elysia` instance on `custom` with the formating function. | ||
```typescript | ||
// ./logger.ts | ||
import { Logestic } from 'logestic'; | ||
const fileLogger = (msg: string) => { | ||
const logFile = Bun.file('requests.log'); | ||
const writer = logFile.writer(); | ||
writer.write(msg); | ||
writer.flush(); | ||
} | ||
// exports an Elysia instance | ||
export new Logestic(fileLogger) | ||
.use(['method', 'path', 'time', 'status']) | ||
.custom(({ method, path, time, status }) => { | ||
return `[${time}]: ${method} ${path} | ${status}` | ||
}) | ||
// ./index.ts | ||
import myLogger from './logger' | ||
const app = new Elysia() | ||
.use(myLogger) | ||
.get('/', () => "Hello from server") | ||
.listen(5566); | ||
``` | ||
Consider contibuting your own preset; check [contributing guidelines](#contributing-guidelines). | ||
## Contributing Guidelines | ||
See [CONTRIBUTING.md](./CONTRIBUTING.md) | ||
## License | ||
[MIT](./LICENSE) | ||
## Authors | ||
- [@cybercoder-naj](https://github.com/cybercoder-naj) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9250
8
101
105
1