@@ -28,6 +28,7 @@ import { FileClientFactory } from "@node-wot/binding-file";
2828import { ThingModelHelpers } from "@thingweb/thing-model" ;
2929import { createContext , Script } from "vm" ;
3030import { CompilerFunction } from "./compiler-function" ;
31+ import { LogLevel , setLogLevel } from "./utils/set-log-level" ;
3132
3233const { debug, error, info } = createLoggers ( "cli" , "cli-default-servient" ) ;
3334
@@ -77,9 +78,6 @@ export default class DefaultServient extends Servient {
7778 coap : {
7879 port : 5683 ,
7980 } ,
80- log : {
81- level : "info" ,
82- } ,
8381 } ;
8482
8583 private uncaughtListeners : Array < NodeJS . UncaughtExceptionListener > = [ ] ;
@@ -103,9 +101,6 @@ export default class DefaultServient extends Servient {
103101 this . config . servient . clientOnly = true ;
104102 }
105103
106- // set log level before any output
107- this . setLogLevel ( this . config . log . level ) ;
108-
109104 // load credentials from config
110105 this . addCredentials ( this . config . credentials ) ;
111106
@@ -249,7 +244,10 @@ export default class DefaultServient extends Servient {
249244 actions : {
250245 setLogLevel : {
251246 description : "Set log level" ,
252- input : { oneOf : [ { type : "string" } , { type : "number" } ] } ,
247+ input : {
248+ type : "string" ,
249+ enum : [ "debug" , "info" , "warn" , "error" ] ,
250+ } ,
253251 output : { type : "string" } ,
254252 } ,
255253 shutdown : {
@@ -268,16 +266,10 @@ export default class DefaultServient extends Servient {
268266 } ,
269267 } ) ;
270268
271- servientProducedThing . setActionHandler ( "setLogLevel" , async ( level ) => {
272- const ll = await Helpers . parseInteractionOutput ( level ) ;
273- if ( typeof ll === "number" ) {
274- this . setLogLevel ( ll as number ) ;
275- } else if ( typeof ll === "string" ) {
276- this . setLogLevel ( ll as string ) ;
277- } else {
278- // try to convert it to strings
279- this . setLogLevel ( ll + "" ) ;
280- }
269+ servientProducedThing . setActionHandler ( "setLogLevel" , async ( payload ) => {
270+ const level = ( await Helpers . parseInteractionOutput ( payload ) ) as LogLevel ;
271+ setLogLevel ( level ) ;
272+ this . logLevel = level ;
281273 return `Log level set to '${ this . logLevel } '` ;
282274 } ) ;
283275 servientProducedThing . setActionHandler ( "shutdown" , async ( ) => {
@@ -310,60 +302,4 @@ export default class DefaultServient extends Servient {
310302 process . removeListener ( "uncaughtException" , listener ) ;
311303 } ) ;
312304 }
313-
314- // Save default loggers (needed when changing log levels)
315- private readonly loggers : any = {
316- warn : console . warn ,
317- info : console . info ,
318- debug : console . debug ,
319- } ;
320-
321- private setLogLevel ( logLevel : string | number ) : void {
322- if ( logLevel === "error" || logLevel === 0 ) {
323- console . warn = ( ) => {
324- /* nothing */
325- } ;
326- console . info = ( ) => {
327- /* nothing */
328- } ;
329- console . debug = ( ) => {
330- /* nothing */
331- } ;
332-
333- this . logLevel = "error" ;
334- } else if ( logLevel === "warn" || logLevel === "warning" || logLevel === 1 ) {
335- console . warn = this . loggers . warn ;
336- console . info = ( ) => {
337- /* nothing */
338- } ;
339- console . debug = ( ) => {
340- /* nothing */
341- } ;
342-
343- this . logLevel = "warn" ;
344- } else if ( logLevel === "info" || logLevel === 2 ) {
345- console . warn = this . loggers . warn ;
346- console . info = this . loggers . info ;
347- console . debug = ( ) => {
348- /* nothing */
349- } ;
350-
351- this . logLevel = "info" ;
352- } else if ( logLevel === "debug" || logLevel === 3 ) {
353- console . warn = this . loggers . warn ;
354- console . info = this . loggers . info ;
355- console . debug = this . loggers . debug ;
356-
357- this . logLevel = "debug" ;
358- } else {
359- // Fallback to default ("info")
360- console . warn = this . loggers . warn ;
361- console . info = this . loggers . info ;
362- console . debug = ( ) => {
363- /* nothing */
364- } ;
365-
366- this . logLevel = "info" ;
367- }
368- }
369305}
0 commit comments