提交 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'
describe('console', () => {
test('console.log format', () => {
......
......@@ -17090,6 +17090,63 @@ function invokeHostEvent(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 = {
load: 'load',
close: 'close',
......@@ -19162,6 +19219,7 @@ var uni$1 = {
navigateToMiniProgram: navigateToMiniProgram,
onHostEventReceive: onHostEventReceive,
onNativeEventReceive: onNativeEventReceive,
__log__: __log__,
navigateTo: navigateTo,
reLaunch: reLaunch,
switchTab: switchTab,
......
......@@ -80,6 +80,7 @@ export {
onHostEventReceive,
onNativeEventReceive,
} from './plugin/sdk'
export { __log__ } from './plugin/log'
export * from './ad/rewardedVideoAd'
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'
const filename = 'foo.vue'
const METHOD = '__f__'
describe('console', () => {
test('console.log', () => {
expect(
rewriteConsoleExpr(filename, filename, `const a = 1;console.log(a);`).code
rewriteConsoleExpr(
METHOD,
filename,
filename,
`const a = 1;console.log(a);`
).code
).toMatchSnapshot()
})
test('console.log multiline', () => {
expect(
rewriteConsoleExpr(
METHOD,
filename,
filename,
`const a = 1;
......@@ -23,22 +30,26 @@ console.log(a,b,c);
})
test('console.info', () => {
expect(
rewriteConsoleExpr(filename, filename, `console.info(a,b,c);`).code
rewriteConsoleExpr(METHOD, filename, filename, `console.info(a,b,c);`)
.code
).toMatchSnapshot()
})
test('console.debug', () => {
expect(
rewriteConsoleExpr(filename, filename, `console.info(a,b,c);`).code
rewriteConsoleExpr(METHOD, filename, filename, `console.info(a,b,c);`)
.code
).toMatchSnapshot()
})
test('console.warn', () => {
expect(
rewriteConsoleExpr(filename, filename, `console.info(a,b,c);`).code
rewriteConsoleExpr(METHOD, filename, filename, `console.info(a,b,c);`)
.code
).toMatchSnapshot()
})
test('console.error', () => {
expect(
rewriteConsoleExpr(filename, filename, `console.info(a,b,c);`).code
rewriteConsoleExpr(METHOD, filename, filename, `console.info(a,b,c);`)
.code
).toMatchSnapshot()
})
})
......@@ -11,8 +11,9 @@ export {
formatInstallHBuilderXPluginTips,
} from './alias'
export function uniHBuilderXConsolePlugin() {
export function uniHBuilderXConsolePlugin(method: string = '__f__') {
return uniConsolePlugin({
method,
filename(filename) {
filename = path.relative(process.env.UNI_INPUT_DIR, filename)
if (filename.startsWith('.') || path.isAbsolute(filename)) {
......
import MagicString from 'magic-string'
import { normalizePath } from '../utils'
const F = '__f__'
export function rewriteConsoleExpr(
method: string,
id: string,
filename: string,
code: string,
......@@ -18,7 +18,7 @@ export function rewriteConsoleExpr(
s.overwrite(
match.index,
match.index + expr.length + 1,
F + `('${type}','at ${filename}:${locate(match.index).line + 1}',`
method + `('${type}','at ${filename}:${locate(match.index).line + 1}',`
)
}
return {
......
......@@ -7,6 +7,7 @@ import { rewriteConsoleExpr } from '../../logs/console'
import { withSourcemap } from '../../vite/utils/utils'
export interface ConsoleOptions {
method: string
filename?: (filename: string) => string
include?: FilterPattern
exclude?: FilterPattern
......@@ -38,6 +39,7 @@ export function uniConsolePlugin(options: ConsoleOptions): Plugin {
}
debugConsole(id)
return rewriteConsoleExpr(
options.method,
id,
filename,
code,
......
......@@ -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) {
const res = normalizeLog(type, filename, args);
res && console[type](res);
// @ts-ignore
uni.__log__ && uni.__log__(type, filename, args);
}
function formatH5Log(type, filename, ...args) {
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 = [
'ad',
......@@ -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) {
const res = normalizeLog(type, filename, args);
res && console[type](res);
// @ts-ignore
uni.__log__ && uni.__log__(type, filename, args);
}
function formatH5Log(type, filename, ...args) {
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(
type: 'log' | 'info' | 'debug' | 'warn' | 'error',
filename: string,
...args: unknown[]
) {
const res = normalizeLog(type, filename, args)
res && console[type](res)
// @ts-ignore
uni.__log__ && uni.__log__(type, filename, args)
}
export function formatH5Log(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册