@zwa73/utils 1.0.28 → 1.0.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DCatchErrorsAsync = exports.DCatchErrors = exports.DLoggerAsync = exports.DLogger = void 0;
4
+ const UtilLogger_1 = require("./UtilLogger");
4
5
  /**用于打印方法的调用
5
6
  * @returns {void}
6
7
  */
@@ -9,7 +10,7 @@ function DLogger() {
9
10
  const originalMethod = descriptor.value;
10
11
  descriptor.value = function (...args) {
11
12
  let result = originalMethod.apply(this, args);
12
- console.log(`Call: ${propertyKey}(${args}) => ${result}`);
13
+ UtilLogger_1.SLogger.info(`Call: ${propertyKey}(${args}) => ${result}`);
13
14
  return result;
14
15
  };
15
16
  };
@@ -23,7 +24,7 @@ function DLoggerAsync() {
23
24
  const originalMethod = descriptor.value;
24
25
  descriptor.value = async function (...args) {
25
26
  let result = await originalMethod.apply(this, args);
26
- console.log(`Call: ${propertyKey}(${args}) => ${result}`);
27
+ UtilLogger_1.SLogger.info(`Call: ${propertyKey}(${args}) => ${result}`);
27
28
  return result;
28
29
  };
29
30
  };
@@ -40,7 +41,7 @@ function DCatchErrors() {
40
41
  originalMethod.apply(this, args);
41
42
  }
42
43
  catch (err) {
43
- console.log(`Error in method ${propertyKey}: ${err}`);
44
+ UtilLogger_1.SLogger.warn(`Error in method ${propertyKey}: ${err}`);
44
45
  }
45
46
  };
46
47
  };
@@ -57,7 +58,7 @@ function DCatchErrorsAsync() {
57
58
  await originalMethod.apply(this, args);
58
59
  }
59
60
  catch (err) {
60
- console.log(`Error in method ${propertyKey}: ${err}`);
61
+ UtilLogger_1.SLogger.warn(`Error in method ${propertyKey}: ${err}`);
61
62
  }
62
63
  };
63
64
  };
@@ -5,6 +5,7 @@ const fluentFfmpeg = require("fluent-ffmpeg");
5
5
  const path = require("path");
6
6
  const fs = require("fs");
7
7
  const UtilClass_1 = require("./UtilClass");
8
+ const UtilLogger_1 = require("./UtilLogger");
8
9
  /**ffmpeg工具类
9
10
  */
10
11
  class SFfmpegTool {
@@ -14,8 +15,6 @@ class SFfmpegTool {
14
15
  let ffmpegPath = process.env.FFMPEG_PATH;
15
16
  if (ffmpegPath != null) {
16
17
  let exepath = path.join(ffmpegPath, "ffmpeg.exe");
17
- //console.log("ffmpegPath")
18
- //console.log(exepath)
19
18
  SFfmpegTool.setFfmpegPath(exepath);
20
19
  }
21
20
  }
@@ -32,12 +31,10 @@ class SFfmpegTool {
32
31
  return new Promise((resolve, reject) => {
33
32
  fluentFfmpeg.ffprobe(inputWavPath, function (err, metadata) {
34
33
  if (err) {
35
- console.log(err);
34
+ UtilLogger_1.SLogger.error("SFfmpegTool.getAudioMetaData 错误", err);
36
35
  resolve(null);
37
36
  }
38
37
  else {
39
- //console.log(inputWavPath+" metadata:");
40
- //console.log(metadata);
41
38
  resolve(metadata);
42
39
  }
43
40
  });
@@ -62,7 +59,7 @@ class SFfmpegTool {
62
59
  await new Promise((resolve, reject) => {
63
60
  fs.unlink(wavPath, function (err) {
64
61
  if (err)
65
- console.error(err);
62
+ UtilLogger_1.SLogger.error("SFfmpegTool.flac2ogg unlink 错误", err);
66
63
  resolve(null);
67
64
  });
68
65
  });
@@ -144,7 +141,7 @@ class SFfmpegTool {
144
141
  await new UtilClass_1.SList(Object.entries(ioMap))
145
142
  .toSStream(cpCount)
146
143
  .map(async ([inPath, outPath]) => {
147
- console.log("正在处理:" + outPath);
144
+ UtilLogger_1.SLogger.info("SFfmpegTool.wav2oggMP 正在处理:" + outPath);
148
145
  await SFfmpegTool.wav2ogg(inPath, outPath, quality);
149
146
  })
150
147
  .appendOperations();
@@ -158,7 +155,7 @@ class SFfmpegTool {
158
155
  await new UtilClass_1.SList(Object.entries(ioMap))
159
156
  .toSStream(cpCount)
160
157
  .map(async ([inPath, outPath]) => {
161
- console.log("正在处理:" + outPath);
158
+ UtilLogger_1.SLogger.info("SFfmpegTool.flac2oggMP 正在处理:" + outPath);
162
159
  await SFfmpegTool.flac2ogg(inPath, outPath, quality);
163
160
  })
164
161
  .appendOperations();
@@ -172,7 +169,7 @@ class SFfmpegTool {
172
169
  await new UtilClass_1.SList(Object.entries(ioMap))
173
170
  .toSStream(cpCount)
174
171
  .map(async ([inPath, outPath]) => {
175
- console.log("正在处理:" + outPath);
172
+ UtilLogger_1.SLogger.info("SFfmpegTool.trimSilenceMP 正在处理:" + outPath);
176
173
  await SFfmpegTool.trimSilence(inPath, outPath, threshold, silence);
177
174
  })
178
175
  .appendOperations();
@@ -186,7 +183,7 @@ class SFfmpegTool {
186
183
  await new UtilClass_1.SList(Object.entries(ioMap))
187
184
  .toSStream(cpCount)
188
185
  .map(async ([inPath, outPath]) => {
189
- console.log("正在处理:" + outPath);
186
+ UtilLogger_1.SLogger.info("SFfmpegTool.resampleMP 正在处理:" + outPath);
190
187
  await SFfmpegTool.resample(inPath, outPath, rate);
191
188
  })
192
189
  .appendOperations();
@@ -54,8 +54,7 @@ async function createPath(filePath, isDir) {
54
54
  return true;
55
55
  }
56
56
  catch (e) {
57
- console.log("createPath 错误");
58
- console.log(e);
57
+ UtilLogger_1.SLogger.error("createPath 错误", e);
59
58
  return false;
60
59
  }
61
60
  }
@@ -78,8 +77,7 @@ function createPathSync(filePath, isDir) {
78
77
  return true;
79
78
  }
80
79
  catch (e) {
81
- console.log("createPath 错误");
82
- console.log(e);
80
+ UtilLogger_1.SLogger.error("createPathSync 错误", e);
83
81
  return false;
84
82
  }
85
83
  }
@@ -155,11 +153,10 @@ async function writeJSONFile(filePath, token) {
155
153
  // 写入文件
156
154
  try {
157
155
  await fs.promises.writeFile(filePath, str);
158
- UtilLogger_1.SLogger.info(`${filePath} writeJSONFile 成功`);
156
+ UtilLogger_1.SLogger.verbose(`${filePath} writeJSONFile 成功`);
159
157
  }
160
158
  catch (err) {
161
- UtilLogger_1.SLogger.error(`${filePath} writeJSONFile 错误`)
162
- .error(err);
159
+ UtilLogger_1.SLogger.error(`${filePath} writeJSONFile 错误`, err);
163
160
  }
164
161
  }
165
162
  exports.writeJSONFile = writeJSONFile;
@@ -4,6 +4,7 @@ exports.SLogger = void 0;
4
4
  const path = require("path");
5
5
  const winston = require("winston");
6
6
  const DailyRotateFile = require("winston-daily-rotate-file");
7
+ const util_1 = require("util");
7
8
  const logLevels = {
8
9
  fatal: 0,
9
10
  error: 1,
@@ -46,14 +47,12 @@ class SLogger {
46
47
  static createLogger(name = "default", consoleLevel = "info", outFloder, fileLevel = "info") {
47
48
  const transports = [];
48
49
  if (outFloder != null) {
49
- const fileFormat = winston.format.combine(winston.format.timestamp({ format: 'YY-MM-DD HH:mm:ss' }), winston.format.printf((info) => {
50
+ const fileFormat = winston.format.combine(winston.format.timestamp({ format: 'HH:mm:ss' }), winston.format.printf((info) => {
50
51
  const level = info.level.toUpperCase();
51
52
  const message = info.message;
52
53
  //格式化
53
54
  let format = `[${info.timestamp}] [${level}]: `;
54
- let space = "";
55
- for (let cha of format)
56
- space += " ";
55
+ let space = " ".repeat(format.length);
57
56
  let messageList = message.split("\n");
58
57
  return `[${info.timestamp}] [${level.toUpperCase()}]: ${messageList.join("\n" + space)}`;
59
58
  }));
@@ -64,15 +63,13 @@ class SLogger {
64
63
  format: fileFormat,
65
64
  }));
66
65
  }
67
- const consoleFormat = winston.format.combine(winston.format.timestamp({ format: 'YY-MM-DD HH:mm:ss' }), winston.format.printf((info) => {
66
+ const consoleFormat = winston.format.combine(winston.format.timestamp({ format: 'HH:mm:ss' }), winston.format.printf((info) => {
68
67
  const level = info.level.toUpperCase();
69
68
  const message = info.message;
70
69
  const colorizedLevel = colorizer.colorize(info.level, level);
71
70
  //格式化
72
71
  let format = `[${info.timestamp}] [${level}]: `;
73
- let space = "";
74
- for (let cha of format)
75
- space += " ";
72
+ let space = " ".repeat(format.length);
76
73
  let messageList = message.split("\n");
77
74
  messageList[0] = colorizer.colorize(info.level, messageList[0]);
78
75
  for (let i = 1; i < messageList.length; i++)
@@ -117,7 +114,7 @@ class SLogger {
117
114
  if (message == null)
118
115
  out = `<${typeof message}> ${typeof message}`;
119
116
  else if (typeof message !== "string")
120
- out = `<${typeof message}> ${message.toString()}`;
117
+ out = `<${typeof message}> ${(0, util_1.inspect)(message)}`;
121
118
  else
122
119
  out = message;
123
120
  strMessages.push(out);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.28",
3
+ "version": "1.0.30",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,3 +1,5 @@
1
+ import { SLogger } from "./UtilLogger";
2
+
1
3
  /**用于打印方法的调用
2
4
  * @returns {void}
3
5
  */
@@ -6,7 +8,7 @@ export function DLogger(){
6
8
  const originalMethod = descriptor.value;
7
9
  descriptor.value = function(...args:any[]){
8
10
  let result = originalMethod.apply(this, args);
9
- console.log(`Call: ${propertyKey}(${args}) => ${result}`);
11
+ SLogger.info(`Call: ${propertyKey}(${args}) => ${result}`);
10
12
  return result;
11
13
  }
12
14
  }
@@ -20,7 +22,7 @@ export function DLoggerAsync(){
20
22
  const originalMethod = descriptor.value;
21
23
  descriptor.value = async function(...args:any[]){
22
24
  let result = await originalMethod.apply(this, args);
23
- console.log(`Call: ${propertyKey}(${args}) => ${result}`);
25
+ SLogger.info(`Call: ${propertyKey}(${args}) => ${result}`);
24
26
  return result;
25
27
  }
26
28
  }
@@ -36,7 +38,7 @@ export function DCatchErrors() {
36
38
  try {
37
39
  originalMethod.apply(this, args);
38
40
  } catch (err) {
39
- console.log(`Error in method ${propertyKey}: ${err}`);
41
+ SLogger.warn(`Error in method ${propertyKey}: ${err}`);
40
42
  }
41
43
  };
42
44
  };
@@ -52,7 +54,7 @@ export function DCatchErrorsAsync() {
52
54
  try {
53
55
  await originalMethod.apply(this, args);
54
56
  } catch (err) {
55
- console.log(`Error in method ${propertyKey}: ${err}`);
57
+ SLogger.warn(`Error in method ${propertyKey}: ${err}`);
56
58
  }
57
59
  };
58
60
  };
@@ -3,6 +3,7 @@ import * as fluentFfmpeg from "fluent-ffmpeg";
3
3
  import * as path from "path";
4
4
  import * as fs from "fs";
5
5
  import { SList, SStream } from "./UtilClass";
6
+ import { SLogger } from "./UtilLogger";
6
7
 
7
8
  /**输入输出路径映射
8
9
  * 输入路径:输入路径
@@ -18,8 +19,6 @@ class SFfmpegTool {
18
19
  let ffmpegPath = process.env.FFMPEG_PATH;
19
20
  if(ffmpegPath!=null){
20
21
  let exepath = path.join(ffmpegPath,"ffmpeg.exe");
21
- //console.log("ffmpegPath")
22
- //console.log(exepath)
23
22
  SFfmpegTool.setFfmpegPath(exepath);
24
23
  }
25
24
  }
@@ -38,11 +37,9 @@ class SFfmpegTool {
38
37
  return new Promise((resolve, reject) => {
39
38
  fluentFfmpeg.ffprobe(inputWavPath, function (err, metadata) {
40
39
  if (err) {
41
- console.log(err);
40
+ SLogger.error("SFfmpegTool.getAudioMetaData 错误",err);
42
41
  resolve(null);
43
42
  } else {
44
- //console.log(inputWavPath+" metadata:");
45
- //console.log(metadata);
46
43
  resolve(metadata);
47
44
  }
48
45
  });
@@ -73,7 +70,7 @@ class SFfmpegTool {
73
70
  await SFfmpegTool.wav2ogg(wavPath, outputOggPath, quality);
74
71
  await new Promise((resolve, reject) => {
75
72
  fs.unlink(wavPath, function (err) {
76
- if (err) console.error(err);
73
+ if (err) SLogger.error("SFfmpegTool.flac2ogg unlink 错误",err);
77
74
  resolve(null);
78
75
  });
79
76
  });
@@ -174,7 +171,7 @@ class SFfmpegTool {
174
171
  await new SList(Object.entries(ioMap))
175
172
  .toSStream(cpCount)
176
173
  .map(async ([inPath, outPath]) => {
177
- console.log("正在处理:" + outPath);
174
+ SLogger.info("SFfmpegTool.wav2oggMP 正在处理:" + outPath);
178
175
  await SFfmpegTool.wav2ogg(inPath, outPath, quality);
179
176
  })
180
177
  .appendOperations();
@@ -192,7 +189,7 @@ class SFfmpegTool {
192
189
  await new SList(Object.entries(ioMap))
193
190
  .toSStream(cpCount)
194
191
  .map(async ([inPath, outPath]) => {
195
- console.log("正在处理:" + outPath);
192
+ SLogger.info("SFfmpegTool.flac2oggMP 正在处理:" + outPath);
196
193
  await SFfmpegTool.flac2ogg(inPath, outPath, quality);
197
194
  })
198
195
  .appendOperations();
@@ -211,7 +208,7 @@ class SFfmpegTool {
211
208
  await new SList(Object.entries(ioMap))
212
209
  .toSStream(cpCount)
213
210
  .map(async ([inPath, outPath]) => {
214
- console.log("正在处理:" + outPath);
211
+ SLogger.info("SFfmpegTool.trimSilenceMP 正在处理:" + outPath);
215
212
  await SFfmpegTool.trimSilence(inPath, outPath, threshold, silence);
216
213
  })
217
214
  .appendOperations();
@@ -226,7 +223,7 @@ class SFfmpegTool {
226
223
  await new SList(Object.entries(ioMap))
227
224
  .toSStream(cpCount)
228
225
  .map(async ([inPath, outPath]) => {
229
- console.log("正在处理:" + outPath);
226
+ SLogger.info("SFfmpegTool.resampleMP 正在处理:" + outPath);
230
227
  await SFfmpegTool.resample(inPath, outPath, rate);
231
228
  })
232
229
  .appendOperations();
@@ -53,8 +53,7 @@ export async function createPath(filePath: string, isDir?:boolean):Promise<boole
53
53
  return true;
54
54
  }
55
55
  catch(e){
56
- console.log("createPath 错误");
57
- console.log(e);
56
+ SLogger.error("createPath 错误",e);
58
57
  return false;
59
58
  }
60
59
  }
@@ -78,8 +77,7 @@ export function createPathSync(filePath: string, isDir?:boolean):boolean{
78
77
  return true;
79
78
  }
80
79
  catch(e){
81
- console.log("createPath 错误");
82
- console.log(e);
80
+ SLogger.error("createPathSync 错误",e);
83
81
  return false;
84
82
  }
85
83
  }
@@ -191,10 +189,9 @@ export async function writeJSONFile(
191
189
  // 写入文件
192
190
  try {
193
191
  await fs.promises.writeFile(filePath, str);
194
- SLogger.info(`${filePath} writeJSONFile 成功`);
192
+ SLogger.verbose(`${filePath} writeJSONFile 成功`);
195
193
  } catch (err) {
196
- SLogger .error(`${filePath} writeJSONFile 错误`)
197
- .error(err as any as string);
194
+ SLogger.error(`${filePath} writeJSONFile 错误`,err);
198
195
  }
199
196
  }
200
197
 
package/src/UtilLogger.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import * as path from 'path';
2
2
  import * as winston from 'winston';
3
3
  import * as DailyRotateFile from 'winston-daily-rotate-file';
4
+ import {inspect} from 'util';
5
+
4
6
 
5
7
  /**log等级 undefined相当于 silly */
6
8
  export type LogLevel = "fatal"|"error"|"warn"|"info"|"http"|"verbose"|"debug"|"silly"|undefined;
@@ -49,15 +51,13 @@ export class SLogger{
49
51
  const transports:winston.transport[]=[];
50
52
  if(outFloder!=null){
51
53
  const fileFormat = winston.format.combine(
52
- winston.format.timestamp({ format: 'YY-MM-DD HH:mm:ss' }),
54
+ winston.format.timestamp({ format: 'HH:mm:ss' }),
53
55
  winston.format.printf((info) => {
54
56
  const level = info.level.toUpperCase();
55
57
  const message = info.message as string;
56
58
  //格式化
57
59
  let format = `[${info.timestamp}] [${level}]: `
58
- let space = "";
59
- for(let cha of format)
60
- space+=" ";
60
+ let space = " ".repeat(format.length);
61
61
  let messageList = message.split("\n");
62
62
  return `[${info.timestamp}] [${level.toUpperCase()}]: ${messageList.join("\n"+space)}`
63
63
  }),
@@ -70,16 +70,14 @@ export class SLogger{
70
70
  }));
71
71
  }
72
72
  const consoleFormat = winston.format.combine(
73
- winston.format.timestamp({ format: 'YY-MM-DD HH:mm:ss' }),
73
+ winston.format.timestamp({ format: 'HH:mm:ss' }),
74
74
  winston.format.printf((info) => {
75
75
  const level = info.level.toUpperCase();
76
76
  const message = info.message as string;
77
77
  const colorizedLevel = colorizer.colorize(info.level, level);
78
78
  //格式化
79
79
  let format = `[${info.timestamp}] [${level}]: `
80
- let space = "";
81
- for(let cha of format)
82
- space+=" ";
80
+ let space = " ".repeat(format.length);
83
81
  let messageList = message.split("\n");
84
82
  messageList[0]=colorizer.colorize(info.level, messageList[0])
85
83
  for(let i=1;i<messageList.length;i++)
@@ -130,7 +128,7 @@ export class SLogger{
130
128
  if(message == null)
131
129
  out = `<${typeof message}> ${typeof message}`;
132
130
  else if(typeof message!=="string")
133
- out=`<${typeof message}> ${message.toString()}`;
131
+ out=`<${typeof message}> ${inspect(message)}`;
134
132
  else out = message;
135
133
  strMessages.push(out);
136
134
  }
package/test.js CHANGED
@@ -31,8 +31,8 @@ async function main(){
31
31
 
32
32
  main();
33
33
  let a= {a:1};
34
- //SLogger.createLogger("default","silly","./testlog","silly")
35
- SLogger.createLogger("default","silly");
34
+ SLogger.createLogger("default","silly","./testlog","silly")
35
+ //SLogger.createLogger("default","silly");
36
36
  SLogger.fatal('This is an fatal message');
37
37
  SLogger.error('This is an error message');
38
38
  SLogger.warn('This is a warning message');
@@ -43,5 +43,5 @@ SLogger.debug('This is a debug message');
43
43
  SLogger.silly('This is a silly message');
44
44
 
45
45
  SLogger.fatal('This is an info message',123,true);
46
- SLogger.info([1,2,3,4]);
46
+ SLogger.info(["1111111111111111111111111111111111111111111111111111111111111111111111111111111\n11111","222222222222222222222222222222222222222222222222222222222222222222222222222222222222"]);
47
47
 
@@ -0,0 +1,28 @@
1
+ [13:05:26] [FATAL]: This is an fatal message
2
+ [13:05:26] [ERROR]: This is an error message
3
+ [13:05:26] [WARN]: This is a warning message
4
+ [13:05:26] [INFO]: This is an info message
5
+ [13:05:26] [HTTP]: This is an http message
6
+ [13:05:26] [VERBOSE]: This is a verbose message
7
+ [13:05:26] [DEBUG]: This is a debug message
8
+ [13:05:26] [SILLY]: This is a silly message
9
+ [13:05:26] [FATAL]: This is an info message
10
+ <number> 123
11
+ <boolean> true
12
+ [13:05:26] [INFO]: <object> [ 1, 2, 3, 4 ]
13
+ [13:06:25] [FATAL]: This is an fatal message
14
+ [13:06:25] [ERROR]: This is an error message
15
+ [13:06:25] [WARN]: This is a warning message
16
+ [13:06:25] [INFO]: This is an info message
17
+ [13:06:25] [HTTP]: This is an http message
18
+ [13:06:25] [VERBOSE]: This is a verbose message
19
+ [13:06:25] [DEBUG]: This is a debug message
20
+ [13:06:25] [SILLY]: This is a silly message
21
+ [13:06:25] [FATAL]: This is an info message
22
+ <number> 123
23
+ <boolean> true
24
+ [13:06:25] [INFO]: <object> [
25
+ '1111111111111111111111111111111111111111111111111111111111111111111111111111111\n' +
26
+ '11111',
27
+ '222222222222222222222222222222222222222222222222222222222222222222222222222222222222'
28
+ ]