Comments (8)
@code0x9 , i do agree that a skip feature would help and bloating the logs degrades developer ux when you're debugging and stuff. however, a lot of the stuff in drash land is made to be extensible which is why a lot of the modules are class-based. i know you don't want to go the inheritance way, but unfortunately there's no other way to accomplish what you want unless:
- you go the inheritance way; or
- @Guergeiro, @ebebbington and i discuss adding the new feature and getting it released (which could take time and we don't want to be a blocker in your development efforts)
@Guergeiro provided the resource inheritance way, but there's also another way: class MyService extends DexterService { ... }
. this way it keeps you from having to modify resources and extending resources in case that's not your jam. here's a simple example (and you can copy-paste this code and it should work when you run it):
import * as Drash from "https://deno.land/x/[email protected]/mod.ts";
import { DexterService } from "https://deno.land/x/[email protected]/src/services/dexter/dexter.ts";
/**
* Class to override DexterService that can skip the logging process for
* specific requests.
*/
class SkippableDexterService extends DexterService {
/**
* Write logs after a resource is hit.
* @param request
* @param response
*/
runAfterResource(request: Drash.Request, response: Drash.Response): void {
if (this.#skip(request)) {
return;
}
super.runAfterResource(request, response);
}
/**
* Write logs before a resource is hit.
* @param request
* @param response
*/
public runBeforeResource(
request: Drash.Request,
response: Drash.Response,
): void {
if (this.#skip(request)) {
return;
}
super.runBeforeResource(request, response);
}
/**
* Should logging be skipped for the request in question?
* @param request
* @returns True if yes, false if no.
*/
#skip(request: Drash.Request): boolean {
const url = new URL(request.url);
if (url.pathname === "/health_check") {
return true;
}
return false;
}
}
// Create the skippable version of DexterService to be plugged into the server
const dexter = new SkippableDexterService({
enabled: true,
method: true,
url: true,
});
/**
* Some basic resource.
*/
class HomeResource extends Drash.Resource {
public paths = ["/", "/home"];
public GET(request: Drash.Request, response: Drash.Response): void {
return response.text("Hello");
}
}
/**
* Some resource that has a path that matches SkippableDexterService#skip list.
*/
class SkipsDexterServiceResource extends Drash.Resource {
public paths = ["/health_check"];
public GET(request: Drash.Request, response: Drash.Response): void {
return response.text("Hello");
}
}
// Create your server and plug in the skippable dexter service
const server = new Drash.Server({
resources: [
HomeResource,
SkipsDexterServiceResource,
],
services: [
dexter,
],
hostname: "0.0.0.0",
port: 1447,
protocol: "http",
});
server.run();
console.log(`Server running at ${server.address}`);
console.log(`Try it out:\n - /home will result in logs\n - /health_check will skip logging`);
from drash.
@crookse Thanks for your explanation and sample code. I'll try to go with inheritance as you recommended.
from drash.
Can't you implement Dexter only for the resources you want with resource level services? That is, if you don't want Dexter on /health_check
, add Dexter to all other resources except /health_check
.
from drash.
of course your suggestion will work. I just wanted to enable dexter on all resources except some special endpoints.
for example, I want to run drash on K8S and with proves. K8S will keep calling prove endpoints and I don't want K8S to fill up my log storage.
by skip feature, I can add dexter by server-level to reduce duplicate resource-level dexter service configurations.
from drash.
If you want to add to Dexter to all resources more automatically:
export abstract class MyBaseResourceWithBetterName extends Drash.Resource {
public services = {
ALL: [
new DexterService(),
],
};
}
class HomeResource extends MyBaseResourceWithBetterName {
public paths = ["/home", "/foo"];
// Uses dexter
}
class HealthCheck extends Drash.Resource {
public paths = ["/health_check"];
// Doesn't use dexter
}
Will this solve your issue? The problem here is bloating the service with extra config props that are not really required. If we accept this one, more will follow.
from drash.
skip
feature is just a utility function which can solve some problems without class inheritance or code duplicates. morgan provides this(https://github.com/expressjs/morgan#skip) and it's quite useful.
And personally, I prefer Composition over inheritance principle so I'd like to solve this by non-inheritance way.
There can be more scenario like "I want to skip log from internal IP" or "I want to skip log when response code is below 400" which skip
feature can help.
from drash.
@code0x9 feel free to let us know how you get on!
from drash.
closing since there hasn't been activity on this for a while. anyone feel free to open for discussion again!
from drash.
Related Issues (20)
- Service.end() keeps the send property set to true HOT 1
- CLEANUP CREW: Need to add/revise doc blocks, file markers, interfaces, etc HOT 1
- chore: use rhum v2 HOT 1
- bug: 406 thrown for empty string accept header
- perf: dont try to find resource if it was already found previously
- feat: graphql service no longer needs empty resource; add option to define playground endpoint
- feat: check for trailing/leading slash in jae
- feat: add connInfo to IErrorHandler? HOT 2
- feat: tengine to work in Deploy
- bug: ResponseTimeService not work when an exception occurs
- BREAKING!: v3-beta HOT 1
- feat(http/server): add new config for control flow changes
- chore(service/tengine): verify Deno Deploy compatibility HOT 1
- bug: Response.download doesn't work with deno deploy HOT 2
- question: k6 tests a lot of fails HOT 2
- Module not found "https://deno.land/x/[email protected]/mod.ts"bug: HOT 2
- feat(v3-beta): finish leftover tasks
- bug(ci): send webhook after CI finishes release, not at the time it runs
- chore: check bundle sizes when drash files are imported
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drash.