Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding advanced logging capability #1227

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,15 @@ If the `log` method is defined, it will be called with 'received' and 'replied'
};
```

Optionally http request or response context access.

``` javascript
server = soap.listen(...)
server.log = function(type, data, req, res) {
// req -> http.IncomingMessage, res -> http.ServerResponse
};
```

### Server Events

Server instances emit the following events:
Expand Down
18 changes: 9 additions & 9 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ interface IExecuteMethodOptions {
export class Server extends EventEmitter {
public path: string | RegExp;
public services: IServices;
public log: (type: string, data: any) => any;
public log: (type: string, data: any, req?: Request, res?: Response) => any;
public authorizeConnection: (req: Request, res?: Response) => boolean;
public authenticate: (security: any, processAuthResult?: (result: boolean) => void, req?: Request, obj?: any) => boolean | void | Promise<boolean>;

Expand Down Expand Up @@ -205,27 +205,27 @@ export class Server extends EventEmitter {
let error;
try {
if (typeof this.log === 'function') {
this.log('received', xml);
this.log('received', xml, req, res);
}
this._process(xml, req, res, (result, statusCode) => {
this._sendHttpResponse(res, statusCode, result);
if (typeof this.log === 'function') {
this.log('replied', result);
this.log('replied', result, req, res);
}
});
} catch (err) {
if (err.Fault !== undefined) {
return this._sendError(err.Fault, (result, statusCode) => {
this._sendHttpResponse(res, statusCode || 500, result);
if (typeof this.log === 'function') {
this.log('error', err);
this.log('error', err, req, res);
}
}, new Date().toISOString());
} else {
error = err.stack ? (this.suppressStack === true ? err.message : err.stack) : err;
this._sendHttpResponse(res, /* statusCode */ 500, error);
if (typeof this.log === 'function') {
this.log('error', error);
this.log('error', error, req, res);
}
}
}
Expand All @@ -237,14 +237,14 @@ export class Server extends EventEmitter {
const reqQuery = reqParse.search;

if (typeof this.log === 'function') {
this.log('info', 'Handling ' + req.method + ' on ' + req.url);
this.log('info', 'Handling ' + req.method + ' on ' + req.url, req, res);
}

if (req.method === 'GET') {

if (reqQuery && reqQuery.toLowerCase().startsWith('?wsdl')) {
if (typeof this.log === 'function') {
this.log('info', 'Wants the WSDL');
this.log('info', 'Wants the WSDL', req, res);
}
res.setHeader('Content-Type', 'application/xml');
res.write(this.wsdl.toXML());
Expand Down Expand Up @@ -314,7 +314,7 @@ export class Server extends EventEmitter {
const process = () => {

if (typeof this.log === 'function') {
this.log('info', 'Attempting to bind to ' + pathname);
this.log('info', 'Attempting to bind to ' + pathname, req, res);
}

// Avoid Cannot convert undefined or null to object due to Object.keys(body)
Expand All @@ -338,7 +338,7 @@ export class Server extends EventEmitter {
const portPathname = url.parse(port.location).pathname.replace(/\/$/, '');

if (typeof this.log === 'function') {
this.log('info', 'Trying ' + portName + ' from path ' + portPathname);
this.log('info', 'Trying ' + portName + ' from path ' + portPathname, req, res);
}

if (portPathname === pathname) {
Expand Down