Skip to content

Commit

Permalink
feat: 使用log4js库记录日志
Browse files Browse the repository at this point in the history
  • Loading branch information
ikenxuan committed Jul 18, 2024
1 parent 7227822 commit cecf4d6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
},
"dependencies": {
"@fastify/static": "^7.0.4",
"chalk": "^5.3.0",
"fastify": "^4.28.1",
"log4js": "^6.9.1",
"node-fetch": "^3.3.2",
"pm2": "^5.4.2",
"yaml": "^2.4.5"
Expand Down
2 changes: 1 addition & 1 deletion src/parse/getdata/judgment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface IDDataTypes {
*/

export default async function GetDouyinID (url: string): Promise<IDDataTypes> {
const longLink = await new networks({ url }).getLocation()
const longLink = await new networks({ url }).getLongLink()
let result: IDDataTypes = {} as IDDataTypes
switch (true) {
case /video\/(\d+)/.test(longLink):
Expand Down
7 changes: 4 additions & 3 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fileURLToPath } from 'url'
import { dirname, join } from 'path'
import { Result } from '@zuks/getdata'
import { DataType, OptionsType } from '@zuks/types'
import { logger } from '@zuks/model'

// 定义请求类型,包括OptionsType的属性
interface MyRequest extends FastifyRequest {
Expand All @@ -12,9 +13,9 @@ interface MyRequest extends FastifyRequest {

export default async function Fastify () {
const __dirname = dirname(fileURLToPath(import.meta.url))
const server = fastify({ logger: true })
server.listen({ port: 4567, host: '::' }, async function (_err: any, address: any) {
console.log(`服务正在监听 ${address}`)
const server = fastify()
server.listen({ port: 4567, host: '127.0.0.1' }, async function (_err: any, address: string) {
logger.mark(`服务正在监听 ${address}`)
})
server.register(fastifyStatic, { root: join(__dirname), prefix: '/static' })

Expand Down
21 changes: 20 additions & 1 deletion src/parse/model/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import networks from './networks';
import cfg from './config';
import log4js from 'log4js';

export { networks, cfg }

log4js.configure({
appenders: {
console: {
type: 'stdout',
layout: {
type: 'pattern',
pattern: '[%[%d{yyyy-MM-dd hh:mm:ss}] [%p] %m%]'
}
}
},
categories: {
default: { appenders: ['console'], level: 'debug' },
},
});

const logger = log4js.getLogger();

export { networks, cfg, logger }
25 changes: 13 additions & 12 deletions src/parse/model/networks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fetch, { Response } from 'node-fetch'
import { NetworksConfigType } from '@zuks/types'
import { logger } from '@zuks/model'


interface HeadersObject {
Expand Down Expand Up @@ -57,12 +58,12 @@ export default class networks {
this.isGetResult = true
return result
} catch (error) {
console.log(error)
(error)
return false
}
}

async returnResult (): Promise<Response> {
async returnResult () {
return await fetch(this.url, this.config)
}

Expand All @@ -72,7 +73,7 @@ export default class networks {
const result = await this.returnResult()
return result.url
} catch (error) {
console.log(error)
logger.error(error)
return ''
}
}
Expand All @@ -87,7 +88,7 @@ export default class networks {
// 取location返回
return response.headers.get('location') as string
} catch (error) {
console.log(error)
logger.error(error)
return ''
}
}
Expand Down Expand Up @@ -121,7 +122,7 @@ export default class networks {
}
return this.fetch
} catch (error) {
console.log(error)
logger.error(error)
return false
}
}
Expand All @@ -140,15 +141,15 @@ export default class networks {
}
return headersObject
} else {
console.log('未获取到响应头')
logger.error('未获取到响应头')
return null
}
} else {
console.log('未获取到响应对象')
logger.error('未获取到响应对象')
return null
}
} catch (error) {
console.log('获取响应头失败:', error)
logger.error('获取响应头失败:', error)
return null
}
}
Expand All @@ -172,7 +173,7 @@ export default class networks {
headers[key] = value
}
} else {
console.log('未获取到响应头')
logger.error('未获取到响应头')
}

// 获取响应数据
Expand All @@ -191,12 +192,12 @@ export default class networks {
break
}
} else {
console.log('未获取到响应对象')
logger.error('未获取到响应对象')
}

return { headers, data }
} catch (error) {
console.log('获取响应头和数据失败:', error)
logger.error('获取响应头和数据失败:', error)
return { headers: null, data: null }
}
}
Expand Down Expand Up @@ -226,7 +227,7 @@ export default class networks {
const controller = new AbortController();
return new Promise<Response>((resolve, reject) => {
this.timer = setTimeout(() => {
console.log('执行力');
logger.mark('执行力');
controller.abort();
resolve(new Response('timeout', { status: 504, statusText: 'timeout' }));
}, timeout);
Expand Down

0 comments on commit cecf4d6

Please sign in to comment.