Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4d7c100

Browse files
authored
Merge pull requestcoderoad#254 from coderoad/improve-logging
Improve logging
2 parents1f8d8bd +b1298cc commit4d7c100

File tree

23 files changed

+85
-61
lines changed

23 files changed

+85
-61
lines changed

‎src/actions/utils/loadWatchers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import*aschokidarfrom'chokidar'
22
import*asvscodefrom'vscode'
33
import{COMMANDS}from'../../editor/commands'
4-
importenvironmentfrom'../../environment'
4+
import{WORKSPACE_ROOT}from'../../environment'
55

66
// NOTE: vscode createFileWatcher doesn't seem to detect changes outside of vscode
77
// such as `npm install` of a package. Went with chokidar instead
@@ -26,7 +26,7 @@ const loadWatchers = (watchers: string[]) => {
2626
// see how glob patterns are used in VSCode (not like a regex)
2727
// https://code.visualstudio.com/api/references/vscode-api#GlobPattern
2828
constfsWatcher:chokidar.FSWatcher=chokidar.watch(watcher,{
29-
cwd:environment.WORKSPACE_ROOT,
29+
cwd:WORKSPACE_ROOT,
3030
interval:1000,
3131
})
3232

‎src/actions/utils/openFiles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const openFiles = async (files: string[]) => {
2020
// ensure the panel is redrawn on the right side first
2121
vscode.commands.executeCommand(COMMANDS.OPEN_WEBVIEW)
2222
}catch(error){
23-
console.log(`Failed to open file${filePath}`,error)
23+
console.log(`Failed to open file${filePath}:${error.message}`)
2424
}
2525
}
2626
}

‎src/actions/utils/runCommands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const runCommands = async (commands: string[], send: (action: T.Action) => void)
1515
try{
1616
result=awaitexec(command)
1717
}catch(error){
18-
console.log(error)
18+
console.log(`Test failed:${error.message}`)
1919
send({type:'COMMAND_FAIL',payload:{process:{ ...process,status:'FAIL'}}})
2020
return
2121
}

‎src/channel/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { openWorkspace, checkWorkspaceEmpty } from '../services/workspace'
1414
import{readFile}from'fs'
1515
import{join}from'path'
1616
import{promisify}from'util'
17-
importenvironmentfrom'../environment'
17+
import{WORKSPACE_ROOT}from'../environment'
1818

1919
constreadFileAsync=promisify(readFile)
2020

@@ -45,12 +45,12 @@ class Channel implements Channel {
4545
constactionType:string=typeofaction==='string' ?action :action.type
4646
// const onError = (error: T.ErrorMessage) => this.send({ type: 'ERROR', payload: { error }})
4747

48-
// console.log(`ACTION:${actionType}`)
48+
logger(`EXT RECEIVED: "${actionType}"`)
4949

5050
switch(actionType){
5151
case'EDITOR_STARTUP':
5252
// check if a workspace is open, otherwise nothing works
53-
constnoActiveWorksapce=!environment.WORKSPACE_ROOT.length
53+
constnoActiveWorksapce=!WORKSPACE_ROOT.length
5454
if(noActiveWorksapce){
5555
consterror:E.ErrorMessage={
5656
type:'NoWorkspaceFound',
@@ -260,7 +260,7 @@ class Channel implements Channel {
260260
})
261261

262262
// log error to console for safe keeping
263-
console.log(`ERROR:\n${errorMarkdown}`)
263+
logger(`ERROR:\n${errorMarkdown}`)
264264

265265
if(errorMarkdown){
266266
// add a clearer error message for the user
@@ -270,6 +270,9 @@ class Channel implements Channel {
270270

271271
// action may be an object.type or plain string
272272
constactionType:string=typeofaction==='string' ?action :action.type
273+
274+
logger(`EXT TO CLIENT: "${actionType}"`)
275+
273276
switch(actionType){
274277
case'TEST_PASS':
275278
consttutorial=this.context.tutorial.get()

‎src/environment.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@ require('dotenv').config({
22
path:'./web-app/.env',
33
})
44

5-
import*asvscodefrom'vscode'
65
import{getWorkspaceRoot}from'./services/workspace'
76

8-
interfaceEnvironment{
9-
VERSION:string
10-
NODE_ENV:string
11-
LOG:boolean
12-
API_URL:string
13-
SENTRY_DSN:string|null
14-
WORKSPACE_ROOT:string
15-
}
7+
// CodeRoad version
8+
exportconstVERSION:string=process.env.npm_package_version||'unknown'
169

17-
constenvironment:Environment={
18-
VERSION:process.env.VERSION||'unknown',
19-
NODE_ENV:process.env.NODE_ENV||'production',
20-
LOG:(process.env.LOG||'').toLowerCase()==='true',
21-
API_URL:process.env.REACT_APP_GQL_URI||'',
22-
SENTRY_DSN:process.env.SENTRY_DSN||null,
23-
WORKSPACE_ROOT:getWorkspaceRoot(),
24-
}
10+
// Node env
11+
exporttypeEnv='test'|'local'|'development'|'production'
12+
//@ts-ignore
13+
exportconstNODE_ENV:Env=process.env.NODE_ENV||'production'
2514

26-
exportdefaultenvironment
15+
// toggle logging in development
16+
exportconstLOG:boolean=
17+
(process.env.REACT_APP_LOG||'').toLowerCase()==='true'&&process.env.NODE_ENV!=='production'
18+
19+
// error logging tool
20+
exportconstSENTRY_DSN:string|null=process.env.SENTRY_DSN||null
21+
22+
// uri path to the users project workspace
23+
exportconstWORKSPACE_ROOT:string=getWorkspaceRoot()

‎src/services/logger/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
importenviromentfrom'../../environment'
1+
import{LOG}from'../../environment'
22

33
constlogger=(message:string|string[])=>{
4-
if(!enviroment.LOG){
4+
if(!LOG){
55
return
66
}
77
if(Array.isArray(message)){

‎src/services/node/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import { exec as cpExec } from 'child_process'
22
import*asfsfrom'fs'
33
import{join}from'path'
44
import{promisify}from'util'
5-
importenvironmentfrom'../../environment'
5+
import{WORKSPACE_ROOT}from'../../environment'
66

77
constasyncExec=promisify(cpExec)
88

99
exportconstexec=(cmd:string):Promise<{stdout:string;stderr:string}>|never=>{
1010
returnasyncExec(cmd,{
11-
cwd:environment.WORKSPACE_ROOT,
11+
cwd:WORKSPACE_ROOT,
1212
})
1313
}
1414

1515
exportconstexists=(...paths:string[]):boolean|never=>{
16-
returnfs.existsSync(join(environment.WORKSPACE_ROOT, ...paths))
16+
returnfs.existsSync(join(WORKSPACE_ROOT, ...paths))
1717
}

‎src/services/sentry/init.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import{init}from'@sentry/node'
2-
importenvironmentfrom'../../environment'
2+
import{SENTRY_DSN,NODE_ENV}from'../../environment'
33

4-
if(environment.SENTRY_DSN){
4+
if(SENTRY_DSN){
55
init({
6-
dsn:environment.SENTRY_DSN,
7-
environment:environment.NODE_ENV,
6+
dsn:SENTRY_DSN,
7+
environment:NODE_ENV,
88
})
99
}

‎src/services/sentry/onError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import*assentryfrom'@sentry/node'
22
// import { Scope } from '@sentry/hub'
3-
importenvironmentfrom'../../environment'
3+
import{VERSION}from'../../environment'
44

55
constonError=(error:Error)=>{
66
// set user scope https://docs.sentry.io/enriching-error-data/scopes/?platform=node
77
sentry.withScope((scope:any)=>{
8-
scope.setTag('VERSION',environment.VERSION)
8+
scope.setTag('VERSION',VERSION)
99
// if (user) {
1010
// scope.setUser({
1111
// id: user.id,

‎src/services/workspace/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import*asvscodefrom'vscode'
22
import*asfsfrom'fs'
33
import{promisify}from'util'
4-
importenvironmentfrom'../../environment'
4+
import{WORKSPACE_ROOT}from'../../environment'
55

66
constreadDir=promisify(fs.readdir)
77

@@ -13,7 +13,7 @@ export const openWorkspace = () => {
1313
exportconstcheckWorkspaceEmpty=async()=>{
1414
letfiles
1515
try{
16-
files=awaitreadDir(environment.WORKSPACE_ROOT)
16+
files=awaitreadDir(WORKSPACE_ROOT)
1717
}catch(error){
1818
thrownewError('Failed to check workspace')
1919
}

‎src/webview/render.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { JSDOM } from 'jsdom'
22
import*aspathfrom'path'
33
import*asvscodefrom'vscode'
44
importonErrorfrom'../services/sentry/onError'
5-
importenvironmentfrom'../environment'
65

76
constgetNonce=():string=>{
87
lettext=''
@@ -73,7 +72,7 @@ async function render(panel: vscode.WebviewPanel, rootPath: string) {
7372
cspMeta.content=
7473
[
7574
`default-src 'self'`,
76-
`connect-src https: http:${environment.API_URL}`,
75+
`connect-src https: http:`,
7776
//@ts-ignore
7877
`font-src${panel.webview.cspSource} http: https: data:`,
7978
//@ts-ignore

‎web-app/.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SKIP_PREFLIGHT_CHECK=true
2+
VERSION=0.1.0
3+
NODE_ENV=local
4+
REACT_APP_DEBUG=false
5+
REACT_APP_LOG=false
6+
REACT_APP_TUTORIAL_LIST_URL=https://raw.githubusercontent.com/coderoad/tutorials/master/tutorials.json

‎web-app/.storybook/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import '../src/styles/index.css'
55
//@ts-ignore
66
global.acquireVsCodeApi=()=>({
77
postMessage(event:string){
8-
console.log('postMessage',event)
8+
console.log('ERROR: VSCode did not load properly in CodeRoad extension',event)
99
},
1010
})
1111

‎web-app/src/components/Error/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const ErrorMarkdown = ({ error, send }: Props) => {
4040
React.useEffect(()=>{
4141
if(error){
4242
// log error
43-
console.log(error)
43+
console.log(`ERROR in markdown:${error.message}`)
4444
}
4545
},[error])
4646

‎web-app/src/components/ErrorBoundary/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import*asReactfrom'react'
22
importonErrorfrom'../../services/sentry/onError'
3+
importloggerfrom'../../services/logger'
34

45
classErrorBoundaryextendsReact.Component{
56
publicstate={errorMessage:null}
@@ -9,8 +10,8 @@ class ErrorBoundary extends React.Component {
910
// Display fallback UI
1011
this.setState({errorMessage:error.message})
1112
// You can also log the error to an error reporting service
12-
console.error(JSON.stringify(error))
13-
console.log(JSON.stringify(info))
13+
logger('ERROR in component:',JSON.stringify(error))
14+
logger('ERROR info:',JSON.stringify(info))
1415
}
1516

1617
publicrender(){

‎web-app/src/components/Markdown/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const Markdown = (props: Props) => {
6767
try{
6868
html=md.render(props.children)
6969
}catch(error){
70-
constmessage=`failed to parse markdown for${props.children}`
70+
constmessage=`Failed to parse markdown for${props.children}`
7171
onError(newError(message))
7272
console.log(message)
7373
html=`<div style='background-color: #FFB81A; padding: 0.5rem;'>

‎web-app/src/components/Router/index.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createMachine } from '../../services/state/machine'
44
import{useMachine}from'../../services/xstate-react'
55
importRoutefrom'./Route'
66
importonErrorfrom'../../services/sentry/onError'
7-
import{LOG_STATE}from'../../environment'
7+
importloggerfrom'../../services/logger'
88

99
interfaceOutput{
1010
context:T.MachineContext
@@ -16,27 +16,30 @@ interface Output {
1616
declareletacquireVsCodeApi:any
1717

1818
consteditor=acquireVsCodeApi()
19+
consteditorSend=(action:T.Action)=>{
20+
logger(`CLIENT TO EXT: "${action.type}"`)
21+
returneditor.postMessage(action)
22+
}
1923

2024
// router finds first state match of <Route path='' />
2125
constuseRouter=():Output=>{
22-
const[state,send]=useMachine<T.MachineContext,any>(createMachine({editorSend:editor.postMessage}))
26+
const[state,send]=useMachine<T.MachineContext,any>(createMachine({ editorSend}))
2327

24-
if(LOG_STATE){
25-
console.log(JSON.stringify(state.value))
26-
}
28+
logger(`STATE:${JSON.stringify(state.value)}`)
2729

2830
// event bus listener
2931
React.useEffect(()=>{
3032
constlistener='message'
3133
// propograte channel event to state machine
32-
consthandler=(action:any)=>{
34+
consthandler=(event:any)=>{
3335
// NOTE: must call event.data, cannot destructure. VSCode acts odd
34-
constevent=action.data
36+
constaction=event.data
3537
// ignore browser events from plugins
36-
if(event.source){
38+
if(action.source){
3739
return
3840
}
39-
send(event)
41+
logger(`CLIENT RECEIVED: "${action.type}"`)
42+
send(action)
4043
}
4144
window.addEventListener(listener,handler)
4245
return()=>{

‎web-app/src/containers/SelectTutorial/LoadTutorialSummary.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const LoadTutorialSummary = (props: Props) => {
1414
return<Loadingtext="Loading tutorial summary..."/>
1515
}
1616
if(error){
17-
console.log(error)
17+
console.log(`Failed to load tutorial summary:${error}`)
1818
return<div>Error loading summary</div>
1919
}
2020
if(!data){

‎web-app/src/containers/SelectTutorial/forms/TutorialUrl.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import*asReactfrom'react'
22
import{Button,Form,Input}from'@alifd/next'
3+
importloggerfrom'../../../services/logger'
34

45
constFormItem=Form.Item
56

@@ -12,7 +13,7 @@ const TutorialUrl = (props: Props) => {
1213
const[url,setUrl]=React.useState(props.defaultUrl)
1314
constonSubmit=(e:any)=>{
1415
e.preventDefault()
15-
console.log('tutorial url',url)
16+
logger(`Tutorial url:${url}`)
1617
props.onTutorialLoad(url)
1718
}
1819

‎web-app/src/environment.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ for (const required of requiredKeys) {
99
exportconstDEBUG:boolean=(process.env.REACT_APP_DEBUG||'').toLowerCase()==='true'
1010
exportconstVERSION:string=process.env.VERSION||'unknown'
1111
exportconstNODE_ENV:string=process.env.NODE_ENV||'development'
12-
exportconstLOG_STATE:boolean=(process.env.REACT_APP_LOG_STATE||'').toLowerCase()==='true'
12+
exportconstLOG:boolean=
13+
(process.env.REACT_APP_LOG||'').toLowerCase()==='true'&&process.env.NODE_ENV!=='production'
1314
exportconstTUTORIAL_LIST_URL:string=process.env.REACT_APP_TUTORIAL_LIST_URL||''
1415
exportconstSENTRY_DSN:string|null=process.env.REACT_APP_SENTRY_DSN||null

‎web-app/src/mock/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ if (!global.acquireVsCodeApi) {
33
//@ts-ignore
44
global.acquireVsCodeApi=()=>({
55
postMessage(event:string){
6-
console.log('postMessage',event)
6+
console.log('VSCode did not load properly for CodeRoad extension',event)
77
},
88
})
99
}

‎web-app/src/services/logger/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import{LOG}from'../../environment'
2+
3+
constlogger=(...messages:string[])=>{
4+
if(!LOG){
5+
return
6+
}
7+
// Inside vscode, you console.log does not allow more than 1 param
8+
// to get around it, we can log with multiple log statements
9+
for(constmessageofmessages){
10+
console.log(message)
11+
}
12+
}
13+
14+
exportdefaultlogger

‎web-app/src/services/sentry/init.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ try {
99
})
1010
}
1111
}catch(error){
12-
console.log('Error in Sentry init')
13-
console.log(error)
12+
console.log(`Error in Sentry init:${error.message}`)
1413
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp