1
+ import * as httpyac from 'httpyac' ;
2
+ import { ResourceConfig , getResourceConfig } from '../config' ;
3
+ import { getOutputChannel , logStream } from '../io' ;
4
+
5
+
6
+ export async function provideOutputChannelLogger ( _env : string [ ] | undefined , context : httpyac . VariableProviderContext ) : Promise < httpyac . Variables > {
7
+
8
+ if ( context . config && isProcessorContext ( context ) ) {
9
+ const resourceConfig = getResourceConfig ( context . httpFile . fileName ) ;
10
+ if ( resourceConfig . logRequest ) {
11
+ const outputChannelLogResponse = httpyac . utils . requestLoggerFactory ( ( arg : string ) => {
12
+ const requestChannel = getOutputChannel ( 'Request' , 'http' ) ;
13
+ requestChannel . appendLine ( arg ) ;
14
+ } , getRequestLoggerOptions ( resourceConfig , context . config ) ) ;
15
+ const logContextStream = context . logStream ;
16
+ context . logStream = async ( type , message ) => {
17
+ await logStream ( type , message ) ;
18
+ if ( logContextStream ) {
19
+ await logContextStream ?.( type , message ) ;
20
+ }
21
+ } ;
22
+ const logResponse = context . logResponse ;
23
+ context . logResponse = async ( response , httpRegion ) => {
24
+ await outputChannelLogResponse ( response , httpRegion ) ;
25
+ await logResponse ?.( response , httpRegion ) ;
26
+ } ;
27
+ }
28
+ }
29
+
30
+ return { }
31
+ }
32
+
33
+ function isProcessorContext ( context : unknown ) : context is httpyac . ProcessorContext {
34
+ const guard = context as httpyac . ProcessorContext ;
35
+ return ! ! guard ?. config ;
36
+ }
37
+
38
+ function getRequestLoggerOptions (
39
+ resourceConfig : ResourceConfig ,
40
+ config : httpyac . EnvironmentConfig
41
+ ) : httpyac . RequestLoggerFactoryOptions | undefined {
42
+ if ( resourceConfig . logOutputChannelOptions || config . log ?. options ) {
43
+ return Object . assign ( { } , resourceConfig . logOutputChannelOptions , config . log ?. options ) ;
44
+ }
45
+ return {
46
+ requestOutput : true ,
47
+ requestHeaders : true ,
48
+ requestBodyLength : 1024 ,
49
+ responseHeaders : true ,
50
+ responseBodyLength : 1024 ,
51
+ } ;
52
+ }
0 commit comments