@@ -6,36 +6,55 @@ import {
66 VulcanExtensionId ,
77 VulcanInternalExtension ,
88} from '../../models/extensions' ;
9- import axios from 'axios' ;
9+ import axios , { AxiosRequestHeaders } from 'axios' ;
10+ import { ConnectionConfig } from '../utils/url' ;
1011
11- interface HttpLoggerConfig {
12+ export interface HttpLoggerConfig {
1213 connection ?: HttpLoggerConnectionConfig ;
1314}
1415
15- interface HttpLoggerConnectionConfig {
16- protocol ?: string | undefined ;
17- host ?: string | undefined ;
18- port ?: number | string ;
19- path ?: string | undefined ;
20- headers ?: NodeJS . Dict < string | string [ ] > | undefined ;
16+ export interface HttpLoggerConnectionConfig extends ConnectionConfig {
17+ headers ?: Record < string , string | number | boolean > | undefined ;
2118}
2219
2320@VulcanInternalExtension ( 'activity-log' )
2421@VulcanExtensionId ( ActivityLoggerType . HTTP_LOGGER )
2522export class HttpLogger extends BaseActivityLogger < HttpLoggerConfig > {
23+ private logger = this . getLogger ( ) ;
24+
2625 public async log ( payload : any ) : Promise < void > {
26+ if ( ! this . isEnabled ( ) ) return ;
2727 const option = this . getOptions ( ) ;
28- if ( ! option ) {
29- throw new Error ( 'Http logger option is not defined. ' ) ;
28+ if ( ! option ?. connection ) {
29+ throw new Error ( 'Http logger connection should be provided ' ) ;
3030 }
31- // TODO-ac: should implement http logger
31+ const headers = option . connection . headers ;
32+ const url = this . getUrl ( option . connection ) ;
3233 try {
3334 // get connection info from option and use axios to send a post requet to the endpoint
34- const { protocol, host, port, path, headers } = option . connection ! ;
35- const url = `${ protocol } ://${ host } :${ port } ${ path } ` ;
36- await axios . post ( url , payload , { headers : headers as any } ) ;
35+ await this . sendActivityLog ( url , payload , headers ) ;
3736 } catch ( err ) {
38- console . error ( err ) ;
37+ this . logger . debug (
38+ `Failed to send activity log to http logger, url: ${ url } `
39+ ) ;
40+ throw err ;
3941 }
4042 }
43+
44+ protected sendActivityLog = async (
45+ url : string ,
46+ payload : JSON ,
47+ headers : AxiosRequestHeaders | undefined
48+ ) : Promise < void > => {
49+ await axios . post ( url , payload , {
50+ headers : headers ,
51+ } ) ;
52+ } ;
53+
54+ protected getUrl = ( connection : HttpLoggerConnectionConfig ) : string => {
55+ const { ssl, host, port, path = '' } = connection ;
56+ const protocol = ssl ? 'https' : 'http' ;
57+ const urlbase = `${ protocol } ://${ host } :${ port } ` ;
58+ return new URL ( path , urlbase ) . href ;
59+ } ;
4160}
0 commit comments