@@ -4,20 +4,31 @@ import {
44 type GraphQLSchema ,
55 type subscribe as SubscribeImplementation ,
66} from 'graphql' ;
7+ import { Logger } from '@graphql-hive/logger' ;
78import { version } from '../version.js' ;
89import { http } from './http-client.js' ;
910import { createPersistedDocuments } from './persisted-documents.js' ;
1011import { createReporting } from './reporting.js' ;
1112import type { HiveClient , HiveInternalPluginOptions , HivePluginOptions } from './types.js' ;
1213import { createUsage } from './usage.js' ;
13- import { createHiveLogger , isLegacyAccessToken } from './utils.js' ;
14+ import { chooseLogger , isLegacyAccessToken } from './utils.js' ;
15+
16+ function resolveLoggerFromConfigOptions ( options : HivePluginOptions ) : Logger {
17+ if ( typeof options . logger == 'string' ) {
18+ return new Logger ( {
19+ level : options . logger ,
20+ } ) ;
21+ }
22+
23+ if ( options . logger instanceof Logger ) {
24+ return options . logger ;
25+ }
26+
27+ return chooseLogger ( options . logger ?? options . agent ?. logger , options . debug ) ;
28+ }
1429
1530export function createHive ( options : HivePluginOptions ) : HiveClient {
16- const logger = createHiveLogger (
17- options ?. agent ?. logger ?? console ,
18- '[hive]' ,
19- options . debug ?? false ,
20- ) ;
31+ const logger = resolveLoggerFromConfigOptions ( options ) . child ( { module : 'hive' } ) ;
2132 let enabled = options . enabled ?? true ;
2233
2334 if ( enabled === false && ! options . experimental__persistedDocuments ) {
@@ -64,8 +75,6 @@ export function createHive(options: HivePluginOptions): HiveClient {
6475 ? options . printTokenInfo === true || ( ! ! options . debug && options . printTokenInfo !== false )
6576 : false ;
6677
67- const infoLogger = createHiveLogger ( logger , '[info]' ) ;
68-
6978 const info = printTokenInfo
7079 ? async ( ) => {
7180 try {
@@ -109,7 +118,7 @@ export function createHive(options: HivePluginOptions): HiveClient {
109118 }
110119 ` ;
111120
112- infoLogger . info ( 'Fetching token details...' ) ;
121+ logger . info ( 'Fetching token details...' ) ;
113122
114123 const clientVersionForDetails = options . agent ?. version || version ;
115124 const response = await http . post (
@@ -128,7 +137,7 @@ export function createHive(options: HivePluginOptions): HiveClient {
128137 } ,
129138 timeout : 30_000 ,
130139 fetchImplementation : options ?. agent ?. fetch ,
131- logger : infoLogger ,
140+ logger,
132141 } ,
133142 ) ;
134143
@@ -160,7 +169,7 @@ export function createHive(options: HivePluginOptions): HiveClient {
160169 const projectUrl = `${ organizationUrl } /${ project . slug } ` ;
161170 const targetUrl = `${ projectUrl } /${ target . slug } ` ;
162171
163- infoLogger . info (
172+ logger . info (
164173 [
165174 'Token details' ,
166175 '' ,
@@ -176,21 +185,17 @@ export function createHive(options: HivePluginOptions): HiveClient {
176185 ] . join ( '\n' ) ,
177186 ) ;
178187 } else if ( result . data ?. tokenInfo . message ) {
179- infoLogger . error ( `Token not found. Reason: ${ result . data ?. tokenInfo . message } ` ) ;
180- infoLogger . info (
181- `How to create a token? https://docs.graphql-hive.com/features/tokens` ,
182- ) ;
188+ logger . error ( `Token not found. Reason: ${ result . data ?. tokenInfo . message } ` ) ;
189+ logger . info ( `How to create a token? https://docs.graphql-hive.com/features/tokens` ) ;
183190 } else {
184- infoLogger . error ( `${ result . errors ! [ 0 ] . message } ` ) ;
185- infoLogger . info (
186- `How to create a token? https://docs.graphql-hive.com/features/tokens` ,
187- ) ;
191+ logger . error ( `${ result . errors ! [ 0 ] . message } ` ) ;
192+ logger . info ( `How to create a token? https://docs.graphql-hive.com/features/tokens` ) ;
188193 }
189194 } else {
190- infoLogger . error ( `Error ${ response . status } : ${ response . statusText } ` ) ;
195+ logger . error ( `Error ${ response . status } : ${ response . statusText } ` ) ;
191196 }
192197 } catch ( error ) {
193- infoLogger . error ( `Error ${ ( error as Error ) ?. message ?? error } ` ) ;
198+ logger . error ( `Error ${ ( error as Error ) ?. message ?? error } ` ) ;
194199 }
195200 }
196201 : ( ) => { } ;
0 commit comments