Skip to content

Commit e7e2e92

Browse files
committed
remove log to own log file
1
1 parent 7daf175 commit e7e2e92

8 files changed

Lines changed: 136 additions & 51 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
node_modules/
33

44
# Build output
5+
main.js
56
*.js.map
67

78
# Obsidian

main.js

Lines changed: 52 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DatabaseView.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ItemView, WorkspaceLeaf, App, TextComponent, DropdownComponent, ButtonC
22
import { DatabaseTable } from './databaseParser';
33
import DatabasePlugin from './main';
44
import { FuzzySuggestModal, TFolder } from 'obsidian';
5+
import { debug, info, warn, error } from './utils/logger';
56

67
export const DATABASE_VIEW_TYPE = 'database-view';
78

@@ -47,7 +48,7 @@ export class DatabaseView extends ItemView {
4748
this.tableStates = tables.map((table, index) => ({ table, id: index + 1, searchTerm: '' }));
4849
this.renderView();
4950
} else {
50-
console.error('setTables 收到无效数据:', tables);
51+
error(`setTables 收到无效数据: ${JSON.stringify(tables).substring(0, 100)}...`);
5152
}
5253
}
5354

@@ -234,7 +235,7 @@ export class DatabaseView extends ItemView {
234235
: [this.tableStates[parseInt(selectedValue)]?.table].filter(Boolean);
235236

236237
if (tablesToExport.length === 0) {
237-
console.error('No tables to export');
238+
error('No tables to export');
238239
return;
239240
}
240241

@@ -307,10 +308,10 @@ export class DatabaseView extends ItemView {
307308
try {
308309
await this.app.vault.create(`${folderPath}/${fileName}`, content);
309310
new Notice(`已创建数据库笔记: ${fileName}`);
310-
} catch (error) {
311-
console.error('创建数据库笔记时出错:', error);
312-
if (isError(error)) {
313-
new Notice(`创建数据库笔记失败: ${error.message}`);
311+
} catch (err) {
312+
error(`创建数据库笔记时出错: ${err instanceof Error ? err.message : String(err)}`);
313+
if (err instanceof Error) {
314+
new Notice(`创建数据库笔记失败: ${err.message}`);
314315
} else {
315316
new Notice('创建数据库笔记失败: 未知错误');
316317
}
@@ -389,7 +390,7 @@ export class DatabaseView extends ItemView {
389390
}
390391

391392
public insertContent(content: string) {
392-
console.log("Inserting content into DatabaseView:", content);
393+
debug(`Inserting content into DatabaseView: ${content.substring(0, 100)}...`);
393394
const newTables = this.parseCSVContent(content);
394395
if (newTables.length > 0) {
395396
newTables.forEach(newTable => {
@@ -402,6 +403,7 @@ export class DatabaseView extends ItemView {
402403
this.renderView();
403404
new Notice(`已在数据库视图中插入 ${newTables.length} 个新表格`);
404405
} else {
406+
warn('无法解析导入的内容');
405407
new Notice('无法解析导入的内容');
406408
}
407409
}

src/databaseParser.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1+
import { debug, info } from './utils/logger';
2+
13
export interface DatabaseTable {
24
name: string;
35
fields: string[];
46
data: string[][];
57
}
68

79
export function parseDatabase(markdown: string): DatabaseTable[] {
8-
console.log('开始解析数据库,输入内容:', markdown);
10+
debug(`开始解析数据库,输入内容: ${markdown.substring(0, 100)}...`);
911
const tables: DatabaseTable[] = [];
1012
const lines = markdown.split('\n');
1113
let currentTable: DatabaseTable | null = null;
1214

1315
for (const line of lines) {
1416
const trimmedLine = line.trim();
15-
console.log('处理行:', trimmedLine);
17+
debug(`处理行: ${trimmedLine}`);
1618
if (trimmedLine.startsWith('db:')) {
17-
console.log('发现新表:', trimmedLine);
19+
debug(`发现新表: ${trimmedLine}`);
1820
if (currentTable) {
1921
tables.push(currentTable);
2022
}
@@ -27,10 +29,10 @@ export function parseDatabase(markdown: string): DatabaseTable[] {
2729
const cells = trimmedLine.split(',').map(cell => cell.trim());
2830
if (cells.length > 1) {
2931
if (currentTable.fields.length === 0) {
30-
console.log('设置字段:', cells);
32+
debug(`设置字段: ${cells.join(', ')}`);
3133
currentTable.fields = cells;
3234
} else {
33-
console.log('添加数据行:', cells);
35+
debug(`添加数据行: ${cells.join(', ')}`);
3436
currentTable.data.push(cells);
3537
}
3638
}
@@ -41,6 +43,6 @@ export function parseDatabase(markdown: string): DatabaseTable[] {
4143
tables.push(currentTable);
4244
}
4345

44-
console.log('解析完成,结果:', tables);
46+
info(`解析完成,结果: ${JSON.stringify(tables).substring(0, 100)}...`);
4547
return tables;
4648
}

src/main.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Plugin, Notice, TFile, MarkdownView, Events, App, PluginManifest, PluginSettingTab, Setting, ButtonComponent } from 'obsidian';
22
import { DatabaseView, DATABASE_VIEW_TYPE } from './DatabaseView';
33
import { parseDatabase, DatabaseTable } from './databaseParser';
4+
import { debug, info, warn, error } from './utils/logger';
45
import '../styles.css';
56

67
interface DatabasePluginSettings {
@@ -17,7 +18,7 @@ export default class DatabasePlugin extends Plugin {
1718

1819
async onload() {
1920
await this.loadSettings();
20-
console.log('加载数据库插件');
21+
info('加载数据库插件');
2122

2223
this.registerView(
2324
DATABASE_VIEW_TYPE,
@@ -69,22 +70,22 @@ export default class DatabasePlugin extends Plugin {
6970
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
7071
if (activeView) {
7172
const content = activeView.getViewData();
72-
console.log('获取到的文件内容:', content);
73+
debug(`获取到的文件内容: ${content}`);
7374
const tables = parseDatabase(content);
74-
console.log('解析后的表格数据:', tables);
75+
debug(`解析后的表格数据: ${JSON.stringify(tables)}`);
7576

7677
if (Array.isArray(tables) && tables.length > 0) {
7778
await this.activateView();
7879
if (this.databaseView) {
79-
console.log('更新数据库视图');
80+
info('更新数据库视图');
8081
this.databaseView.setTables(tables);
8182
new Notice('数据库视图已更新');
8283
} else {
83-
console.error('无法创建或获取数据库视图');
84+
error('无法创建或获取数据库视图');
8485
new Notice('更新数据库视图失败');
8586
}
8687
} else {
87-
console.error('解析结果无效:', tables);
88+
error(`解析结果无效: ${JSON.stringify(tables)}`);
8889
new Notice('解析数据库失败,请检查文件格式');
8990
}
9091
} else {
@@ -104,20 +105,19 @@ export default class DatabasePlugin extends Plugin {
104105
await new Promise(resolve => setTimeout(resolve, 100));
105106

106107
this.databaseView = leaf.view as DatabaseView;
107-
console.log('数据库视图已激活:', this.databaseView);
108+
info(`数据库视图已激活: ${this.databaseView ? 'success' : 'fail'}`);
108109

109110
if (!this.databaseView) {
110-
console.error('激活数据库视图失败');
111+
error('激活数据库视图失败');
111112
new Notice('无法创建数据库视图');
112113
}
113114
}
114115

115116
onunload() {
116-
console.log('卸载数据库插件');
117+
info('卸载数据库插件');
117118
}
118119

119120
async saveData() {
120-
121121
await this.saveSettings();
122122
}
123123

src/utils/logger.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
enum LogLevel {
2+
DEBUG = 0,
3+
INFO = 1,
4+
WARN = 2,
5+
ERROR = 3
6+
}
7+
8+
let currentLogLevel: LogLevel = LogLevel.INFO;
9+
10+
function setLogLevel(level: LogLevel): void {
11+
if (Object.values(LogLevel).includes(level)) {
12+
currentLogLevel = level;
13+
} else {
14+
console.error('无效的日志级别');
15+
}
16+
}
17+
18+
function log(level: LogLevel, message: string): void {
19+
if (level >= currentLogLevel) {
20+
const timestamp = new Date().toISOString();
21+
const levelName = LogLevel[level];
22+
console.log(`[${timestamp}] [${levelName}] ${message}`);
23+
}
24+
}
25+
26+
function debug(message: string): void {
27+
log(LogLevel.DEBUG, message);
28+
}
29+
30+
function info(message: string): void {
31+
log(LogLevel.INFO, message);
32+
}
33+
34+
function warn(message: string): void {
35+
log(LogLevel.WARN, message);
36+
}
37+
38+
function error(message: string): void {
39+
log(LogLevel.ERROR, message);
40+
}
41+
42+
export {
43+
LogLevel,
44+
setLogLevel,
45+
debug,
46+
info,
47+
warn,
48+
error
49+
};

styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,6 @@
160160

161161

162162

163+
163164

164165

tsconfig.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
"ES7",
1919
"ES2021"
2020
],
21-
"strict": true
21+
"strict": true,
22+
"esModuleInterop": true,
23+
"resolveJsonModule": true,
24+
"allowSyntheticDefaultImports": true
2225
},
2326
"include": [
24-
"src/**/*.ts"
27+
"src/**/*.ts",
28+
"src/**/*.js"
2529
]
2630
}

0 commit comments

Comments
 (0)