提交 4b6a0ea7 编写于 作者: fxy060608's avatar fxy060608

refactor(app): log

上级 1a5979eb
import { normalizeLog } from '../../src/hbx/formatLog' import { normalizeLog } from '../../src/service/api/plugin/log'
const filename = 'foo.vue' const filename = 'foo.vue'
describe('console', () => { describe('console', () => {
test('console.log format', () => { test('console.log format', () => {
......
...@@ -17090,6 +17090,63 @@ function invokeHostEvent(event, data) { ...@@ -17090,6 +17090,63 @@ function invokeHostEvent(event, data) {
hostEventCallbacks.forEach((fn) => fn(event, data)); hostEventCallbacks.forEach((fn) => fn(event, data));
} }
function __log__(type, filename, ...args) {
const res = normalizeLog(type, filename, args);
res && console[type](res);
}
function isDebugMode() {
// @ts-expect-error
return typeof __channelId__ === 'string' && __channelId__;
}
function jsonStringifyReplacer(k, p) {
switch (toRawType(p)) {
case 'Function':
return 'function() { [native code] }';
default:
return p;
}
}
function normalizeLog(type, filename, args) {
if (isDebugMode()) {
args.push(filename.replace('at ', 'uni-app:///'));
return console[type].apply(console, args);
}
const msgs = args.map(function (v) {
const type = toTypeString(v).toLowerCase();
if (['[object object]', '[object array]', '[object module]'].indexOf(type) !==
-1) {
try {
v =
'---BEGIN:JSON---' +
JSON.stringify(v, jsonStringifyReplacer) +
'---END:JSON---';
}
catch (e) {
v = type;
}
}
else {
if (v === null) {
v = '---NULL---';
}
else if (v === undefined) {
v = '---UNDEFINED---';
}
else {
const vType = toRawType(v).toUpperCase();
if (vType === 'NUMBER' || vType === 'BOOLEAN') {
v = '---BEGIN:' + vType + '---' + v + '---END:' + vType + '---';
}
else {
v = String(v);
}
}
}
return v;
});
return msgs.join('---COMMA---') + ' ' + filename;
}
const EventType = { const EventType = {
load: 'load', load: 'load',
close: 'close', close: 'close',
...@@ -19162,6 +19219,7 @@ var uni$1 = { ...@@ -19162,6 +19219,7 @@ var uni$1 = {
navigateToMiniProgram: navigateToMiniProgram, navigateToMiniProgram: navigateToMiniProgram,
onHostEventReceive: onHostEventReceive, onHostEventReceive: onHostEventReceive,
onNativeEventReceive: onNativeEventReceive, onNativeEventReceive: onNativeEventReceive,
__log__: __log__,
navigateTo: navigateTo, navigateTo: navigateTo,
reLaunch: reLaunch, reLaunch: reLaunch,
switchTab: switchTab, switchTab: switchTab,
......
...@@ -80,6 +80,7 @@ export { ...@@ -80,6 +80,7 @@ export {
onHostEventReceive, onHostEventReceive,
onNativeEventReceive, onNativeEventReceive,
} from './plugin/sdk' } from './plugin/sdk'
export { __log__ } from './plugin/log'
export * from './ad/rewardedVideoAd' export * from './ad/rewardedVideoAd'
export * from './ad/fullScreenVideoAd' export * from './ad/fullScreenVideoAd'
......
import { toTypeString, toRawType } from '@vue/shared'
export function __log__(
type: 'log' | 'info' | 'debug' | 'warn' | 'error',
filename: string,
...args: unknown[]
) {
const res = normalizeLog(type, filename, args)
res && console[type](res)
}
function isDebugMode() {
// @ts-expect-error
return typeof __channelId__ === 'string' && __channelId__
}
function jsonStringifyReplacer(k: string, p: unknown) {
switch (toRawType(p)) {
case 'Function':
return 'function() { [native code] }'
default:
return p
}
}
export function normalizeLog(
type: 'log' | 'info' | 'debug' | 'warn' | 'error',
filename: string,
args: unknown[]
) {
if (isDebugMode()) {
args.push(filename.replace('at ', 'uni-app:///'))
return console[type].apply(console, args)
}
const msgs = args.map(function (v) {
const type = toTypeString(v).toLowerCase()
if (
['[object object]', '[object array]', '[object module]'].indexOf(type) !==
-1
) {
try {
v =
'---BEGIN:JSON---' +
JSON.stringify(v, jsonStringifyReplacer) +
'---END:JSON---'
} catch (e) {
v = type
}
} else {
if (v === null) {
v = '---NULL---'
} else if (v === undefined) {
v = '---UNDEFINED---'
} else {
const vType = toRawType(v).toUpperCase()
if (vType === 'NUMBER' || vType === 'BOOLEAN') {
v = '---BEGIN:' + vType + '---' + v + '---END:' + vType + '---'
} else {
v = String(v)
}
}
}
return v
})
return msgs.join('---COMMA---') + ' ' + filename
}
import { rewriteConsoleExpr } from '../src/logs/console' import { rewriteConsoleExpr } from '../src/logs/console'
const filename = 'foo.vue' const filename = 'foo.vue'
const METHOD = '__f__'
describe('console', () => { describe('console', () => {
test('console.log', () => { test('console.log', () => {
expect( expect(
rewriteConsoleExpr(filename, filename, `const a = 1;console.log(a);`).code rewriteConsoleExpr(
METHOD,
filename,
filename,
`const a = 1;console.log(a);`
).code
).toMatchSnapshot() ).toMatchSnapshot()
}) })
test('console.log multiline', () => { test('console.log multiline', () => {
expect( expect(
rewriteConsoleExpr( rewriteConsoleExpr(
METHOD,
filename, filename,
filename, filename,
`const a = 1; `const a = 1;
...@@ -23,22 +30,26 @@ console.log(a,b,c); ...@@ -23,22 +30,26 @@ console.log(a,b,c);
}) })
test('console.info', () => { test('console.info', () => {
expect( expect(
rewriteConsoleExpr(filename, filename, `console.info(a,b,c);`).code rewriteConsoleExpr(METHOD, filename, filename, `console.info(a,b,c);`)
.code
).toMatchSnapshot() ).toMatchSnapshot()
}) })
test('console.debug', () => { test('console.debug', () => {
expect( expect(
rewriteConsoleExpr(filename, filename, `console.info(a,b,c);`).code rewriteConsoleExpr(METHOD, filename, filename, `console.info(a,b,c);`)
.code
).toMatchSnapshot() ).toMatchSnapshot()
}) })
test('console.warn', () => { test('console.warn', () => {
expect( expect(
rewriteConsoleExpr(filename, filename, `console.info(a,b,c);`).code rewriteConsoleExpr(METHOD, filename, filename, `console.info(a,b,c);`)
.code
).toMatchSnapshot() ).toMatchSnapshot()
}) })
test('console.error', () => { test('console.error', () => {
expect( expect(
rewriteConsoleExpr(filename, filename, `console.info(a,b,c);`).code rewriteConsoleExpr(METHOD, filename, filename, `console.info(a,b,c);`)
.code
).toMatchSnapshot() ).toMatchSnapshot()
}) })
}) })
...@@ -11,8 +11,9 @@ export { ...@@ -11,8 +11,9 @@ export {
formatInstallHBuilderXPluginTips, formatInstallHBuilderXPluginTips,
} from './alias' } from './alias'
export function uniHBuilderXConsolePlugin() { export function uniHBuilderXConsolePlugin(method: string = '__f__') {
return uniConsolePlugin({ return uniConsolePlugin({
method,
filename(filename) { filename(filename) {
filename = path.relative(process.env.UNI_INPUT_DIR, filename) filename = path.relative(process.env.UNI_INPUT_DIR, filename)
if (filename.startsWith('.') || path.isAbsolute(filename)) { if (filename.startsWith('.') || path.isAbsolute(filename)) {
......
import MagicString from 'magic-string' import MagicString from 'magic-string'
import { normalizePath } from '../utils' import { normalizePath } from '../utils'
const F = '__f__'
export function rewriteConsoleExpr( export function rewriteConsoleExpr(
method: string,
id: string, id: string,
filename: string, filename: string,
code: string, code: string,
...@@ -18,7 +18,7 @@ export function rewriteConsoleExpr( ...@@ -18,7 +18,7 @@ export function rewriteConsoleExpr(
s.overwrite( s.overwrite(
match.index, match.index,
match.index + expr.length + 1, match.index + expr.length + 1,
F + `('${type}','at ${filename}:${locate(match.index).line + 1}',` method + `('${type}','at ${filename}:${locate(match.index).line + 1}',`
) )
} }
return { return {
......
...@@ -7,6 +7,7 @@ import { rewriteConsoleExpr } from '../../logs/console' ...@@ -7,6 +7,7 @@ import { rewriteConsoleExpr } from '../../logs/console'
import { withSourcemap } from '../../vite/utils/utils' import { withSourcemap } from '../../vite/utils/utils'
export interface ConsoleOptions { export interface ConsoleOptions {
method: string
filename?: (filename: string) => string filename?: (filename: string) => string
include?: FilterPattern include?: FilterPattern
exclude?: FilterPattern exclude?: FilterPattern
...@@ -38,6 +39,7 @@ export function uniConsolePlugin(options: ConsoleOptions): Plugin { ...@@ -38,6 +39,7 @@ export function uniConsolePlugin(options: ConsoleOptions): Plugin {
} }
debugConsole(id) debugConsole(id)
return rewriteConsoleExpr( return rewriteConsoleExpr(
options.method,
id, id,
filename, filename,
code, code,
......
...@@ -654,61 +654,9 @@ function parseUrl(url) { ...@@ -654,61 +654,9 @@ function parseUrl(url) {
}; };
} }
function isDebugMode() {
// @ts-expect-error
return typeof __channelId__ === 'string' && __channelId__;
}
function jsonStringifyReplacer(k, p) {
switch (shared.toRawType(p)) {
case 'Function':
return 'function() { [native code] }';
default:
return p;
}
}
function normalizeLog(type, filename, args) {
if (isDebugMode()) {
args.push(filename.replace('at ', 'uni-app:///'));
return console[type].apply(console, args);
}
const msgs = args.map(function (v) {
const type = shared.toTypeString(v).toLowerCase();
if (['[object object]', '[object array]', '[object module]'].indexOf(type) !==
-1) {
try {
v =
'---BEGIN:JSON---' +
JSON.stringify(v, jsonStringifyReplacer) +
'---END:JSON---';
}
catch (e) {
v = type;
}
}
else {
if (v === null) {
v = '---NULL---';
}
else if (v === undefined) {
v = '---UNDEFINED---';
}
else {
const vType = shared.toRawType(v).toUpperCase();
if (vType === 'NUMBER' || vType === 'BOOLEAN') {
v = '---BEGIN:' + vType + '---' + v + '---END:' + vType + '---';
}
else {
v = String(v);
}
}
}
return v;
});
return msgs.join('---COMMA---') + ' ' + filename;
}
function formatAppLog(type, filename, ...args) { function formatAppLog(type, filename, ...args) {
const res = normalizeLog(type, filename, args); // @ts-ignore
res && console[type](res); uni.__log__ && uni.__log__(type, filename, args);
} }
function formatH5Log(type, filename, ...args) { function formatH5Log(type, filename, ...args) {
console[type].apply(console, [...args, filename]); console[type].apply(console, [...args, filename]);
......
import { isHTMLTag, isSVGTag, hyphenate, camelize, isString, isFunction, isPlainObject, extend, isArray, toTypeString, toRawType, capitalize } from '@vue/shared'; import { isHTMLTag, isSVGTag, hyphenate, camelize, isString, isFunction, isPlainObject, extend, isArray, capitalize } from '@vue/shared';
const BUILT_IN_TAG_NAMES = [ const BUILT_IN_TAG_NAMES = [
'ad', 'ad',
...@@ -650,61 +650,9 @@ function parseUrl(url) { ...@@ -650,61 +650,9 @@ function parseUrl(url) {
}; };
} }
function isDebugMode() {
// @ts-expect-error
return typeof __channelId__ === 'string' && __channelId__;
}
function jsonStringifyReplacer(k, p) {
switch (toRawType(p)) {
case 'Function':
return 'function() { [native code] }';
default:
return p;
}
}
function normalizeLog(type, filename, args) {
if (isDebugMode()) {
args.push(filename.replace('at ', 'uni-app:///'));
return console[type].apply(console, args);
}
const msgs = args.map(function (v) {
const type = toTypeString(v).toLowerCase();
if (['[object object]', '[object array]', '[object module]'].indexOf(type) !==
-1) {
try {
v =
'---BEGIN:JSON---' +
JSON.stringify(v, jsonStringifyReplacer) +
'---END:JSON---';
}
catch (e) {
v = type;
}
}
else {
if (v === null) {
v = '---NULL---';
}
else if (v === undefined) {
v = '---UNDEFINED---';
}
else {
const vType = toRawType(v).toUpperCase();
if (vType === 'NUMBER' || vType === 'BOOLEAN') {
v = '---BEGIN:' + vType + '---' + v + '---END:' + vType + '---';
}
else {
v = String(v);
}
}
}
return v;
});
return msgs.join('---COMMA---') + ' ' + filename;
}
function formatAppLog(type, filename, ...args) { function formatAppLog(type, filename, ...args) {
const res = normalizeLog(type, filename, args); // @ts-ignore
res && console[type](res); uni.__log__ && uni.__log__(type, filename, args);
} }
function formatH5Log(type, filename, ...args) { function formatH5Log(type, filename, ...args) {
console[type].apply(console, [...args, filename]); console[type].apply(console, [...args, filename]);
......
import { toTypeString, toRawType } from '@vue/shared'
function isDebugMode() {
// @ts-expect-error
return typeof __channelId__ === 'string' && __channelId__
}
function jsonStringifyReplacer(k: string, p: unknown) {
switch (toRawType(p)) {
case 'Function':
return 'function() { [native code] }'
default:
return p
}
}
export function normalizeLog(
type: 'log' | 'info' | 'debug' | 'warn' | 'error',
filename: string,
args: unknown[]
) {
if (isDebugMode()) {
args.push(filename.replace('at ', 'uni-app:///'))
return console[type].apply(console, args)
}
const msgs = args.map(function (v) {
const type = toTypeString(v).toLowerCase()
if (
['[object object]', '[object array]', '[object module]'].indexOf(type) !==
-1
) {
try {
v =
'---BEGIN:JSON---' +
JSON.stringify(v, jsonStringifyReplacer) +
'---END:JSON---'
} catch (e) {
v = type
}
} else {
if (v === null) {
v = '---NULL---'
} else if (v === undefined) {
v = '---UNDEFINED---'
} else {
const vType = toRawType(v).toUpperCase()
if (vType === 'NUMBER' || vType === 'BOOLEAN') {
v = '---BEGIN:' + vType + '---' + v + '---END:' + vType + '---'
} else {
v = String(v)
}
}
}
return v
})
return msgs.join('---COMMA---') + ' ' + filename
}
export function formatAppLog( export function formatAppLog(
type: 'log' | 'info' | 'debug' | 'warn' | 'error', type: 'log' | 'info' | 'debug' | 'warn' | 'error',
filename: string, filename: string,
...args: unknown[] ...args: unknown[]
) { ) {
const res = normalizeLog(type, filename, args) // @ts-ignore
res && console[type](res) uni.__log__ && uni.__log__(type, filename, args)
} }
export function formatH5Log( export function formatH5Log(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册