Skip to content

Commit

Permalink
Changed to use Logger instead of console.log().
Browse files Browse the repository at this point in the history
  • Loading branch information
banban525 committed Oct 21, 2023
1 parent 3b5160f commit 525a81a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
15 changes: 7 additions & 8 deletions server/EchoNetLiteRawController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DeviceDetailsType, eldata,rinfo } from "echonet-lite";
import { Command, CommandResponse, Response, ELSV, EchoNetCommunicator, RawDataSet } from "./EchoNetCommunicator";
import { Logger } from "./Logger";


export interface CommandWithCallback extends Command
Expand Down Expand Up @@ -93,17 +94,17 @@ export class EchoNetLiteRawController {
res = await EchoNetCommunicator.execCommandPromise(ip, '0ef001', eoj, ELSV.GET, epc, "");
}
catch (e) {
console.log(e);
Logger.warn("[ECHONETLite][raw]", `error getProperty: timeout ${ip} ${eoj} ${epc}`, {exception:e});
return undefined;
}
if (res.responses[0].els.ESV !== ELSV.GET_RES) {
console.log(`error GET ${ip} ${eoj} ${epc}`);
Logger.warn("[ECHONETLite][raw]", `error getProperty: returned ${res.responses[0].els.ESV} ${ip} ${eoj} ${epc}`);
return undefined;
}

const data = res.responses[0].els.DETAILs;
if ((epc in data) === false) {
console.log(`error GET ${ip} ${eoj} ${epc}`);
Logger.warn("[ECHONETLite][raw]", `error getProperty: data not found. ${ip} ${eoj} ${epc}`);
return undefined;
}
return data[epc];
Expand All @@ -126,7 +127,7 @@ export class EchoNetLiteRawController {
res = await EchoNetCommunicator.getMultiPropertyPromise(result.ip, '0ef001', device.eoj, ELSV.GET, ["9d", "9e", "9f"]);
}
catch (e) {
console.log(e);
Logger.warn("[ECHONETLite][raw]", `error getNewNode: timeout ${result.ip} ${device.eoj}`, {exception:e});
continue;
}

Expand Down Expand Up @@ -218,8 +219,7 @@ export class EchoNetLiteRawController {
}
const matchProperty = device.properties.find(_ => _.epc === epc);
if (matchProperty === undefined) {
console.log(`error GET ${result.ip} ${device.eoj} ${epc}`);
continue;
throw Error("ありえない");
}
matchProperty.value = value;
}
Expand Down Expand Up @@ -365,8 +365,7 @@ export class EchoNetLiteRawController {
}
const matchProperty = this.findProperty(property.ip, property.eoj, property.epc);
if (matchProperty === undefined) {
console.log(`error GET ${property.ip} ${property.eoj} ${property.epc}`);
continue;
throw Error("ありえない");
}

const oldValue = matchProperty.value;
Expand Down
44 changes: 16 additions & 28 deletions server/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,48 +48,36 @@ const unhandledErrorLoggerForConsole = winston.createLogger({

export class Logger
{
public static error = (category:string,text:string)=>
public static error = (category:string,text:string, meta:{[key:string]:unknown} = {})=>
{
if(category === "")
if(category !== "")
{
logger.error(text);
}
else
{
logger.error(text, {category});
meta["category"] = category;
}
logger.error(text, meta);
}
public static warn = (category:string,text:string)=>
public static warn = (category:string,text:string, meta:{[key:string]:unknown} = {})=>
{
if(category === "")
{
logger.warn(text);
}
else
if(category !== "")
{
logger.warn(text, {category});
meta["category"] = category;
}
logger.warn(text, meta);
}
public static info = (category:string,text:string)=>
public static info = (category:string,text:string, meta:{[key:string]:unknown} = {})=>
{
if(category === "")
if(category !== "")
{
logger.info(text);
}
else
{
logger.info(text, {category});
meta["category"] = category;
}
logger.info(text, meta);
}
public static debug = (category:string,text:string)=>
public static debug = (category:string,text:string, meta:{[key:string]:unknown} = {})=>
{
if(category === "")
{
logger.debug(text);
}
else
if(category !== "")
{
logger.debug(text, {category});
meta["category"] = category;
}
logger.debug(text, meta);
}
}

0 comments on commit 525a81a

Please sign in to comment.