简体   繁体   English

如何在 Google Application Script 中调试 web 开发?

[英]How to debug web development in Google Application Script?

I am testing/learning webhook.我正在测试/学习 webhook。 How to send, receive it.如何发送,接收它。 So I thought I would use GAS.所以我想我会使用 GAS。

I have this simple script and I wonder why Logger does not work.我有这个简单的脚本,我想知道为什么Logger不起作用。 In Projects/Executions I can see that the script doPost was executed but no logs.Projects/Executions中,我可以看到脚本 doPost 已执行但没有日志。 Email was sent and the script returned the value. Email 已发送,脚本返回值。

  • using old, Legacy Editor (no idea how to get the new one)使用旧的旧版编辑器(不知道如何获得新的)
  • in Menu-Resources-Cloud Platform Project is said "This script has an Apps Script-managed Cloud Platform project."在 Menu-Resources-Cloud Platform Project 中说“这个脚本有一个 Apps Script-managed Cloud Platform 项目”。
  • when I open the project in editor I get this message "This project is running on our new Apps Script runtime powered by Chrome V8."当我在编辑器中打开项目时,我收到此消息“该项目正在我们由 Chrome V8 提供支持的新 Apps 脚本运行时上运行。”
  • exception logging set to "exceptionLogging": "STACKDRIVER" is set to default异常日志记录设置为“exceptionLogging”:“STACKDRIVER”设置为默认值

I tried console.log(e);我试过console.log(e); but it did not work for me.但这对我不起作用。

function doPost(e) {
  
 Logger.log("I was called")
 if(typeof e !== 'undefined'){

    Logger.log(e.parameter);
    Logger.log("I was called 2")
 
    MailApp.sendEmail({
     to: "radek@gmail.com",
     subject: "Call Sucessful",
     htmlBody: "I am your <br>" +
               JSON.stringify(e)+ "<br><br>" +
               JSON.stringify(e.parameters)
      
    });
    
  }  
  return ContentService.createTextOutput(JSON.stringify(e))
}

Question1: Can I make Logger work?问题 1:我可以让 Logger 工作吗?

Question2: I would like to see accepted data in Debugger, is that possible?问题 2:我想在 Debugger 中查看接受的数据,可以吗?

Question3: Is there any way the GAS pushes the data it received to my web browser.问题3: GAS有没有什么办法把它收到的数据推送到我的web浏览器。 Of course the browser is NOT sending the data.当然,浏览器不会发送数据。

Question4: No related to the topic but... Would you know what I need to do in order to be able to use new Editor?问题 4:主题无关,但是...您知道我需要做什么才能使用新的编辑器吗?

If you want your own custom log information to go to Stackdriver, then you need to create a Google Cloud Platform project, and associate that GCP project with your Apps Script project.如果您希望自己的自定义日志信息到 go 到 Stackdriver,那么您需要创建一个 Google Cloud Platform 项目,并将该 GCP 项目与您的 Apps Script 项目相关联。

First create a new GCP Project: Go to your GCP dashboard: https://console.cloud.google.com/home/dashboard?authuser=0首先创建一个新的 GCP 项目:Go 到您的 GCP 仪表板: https://console.cloud.google.com/home/dashboard?authuser=0

In the blue bar at the top, there is a drop down menu for project names.在顶部的蓝色栏中,有一个项目名称的下拉菜单。 Click that, and a dialog should appear with a button to create a new project.单击它,应该会出现一个对话框,其中包含一个创建新项目的按钮。 Create a new Google Cloud Platform project.创建一个新的 Google Cloud Platform 项目。 Copy the Project Number.复制项目编号。

Go back to the Apps Script editor. Go 返回 Apps 脚本编辑器。 From the legacy Apps Script editor, click the "Resources" menu.在旧版 Apps 脚本编辑器中,点击“资源”菜单。 Click Cloud Platform project.单击云平台项目。 Paste in the Project Number and click the button.粘贴项目编号并单击按钮。

Now, any console.log() statements you have will send the logs to Stackdriver.现在,您拥有的任何 console.log() 语句都会将日志发送到 Stackdriver。 And Stackdriver can be viewed in your browser.并且可以在浏览器中查看 Stackdriver。

Note: Some people set up their own logging system to log information from server side Apps Script code to a Google Sheet.注意:有些人设置了自己的日志记录系统,将服务器端 Apps 脚本代码中的信息记录到 Google 表格中。 There are some open source repos that are available.有一些可用的开源存储库。

The new code editor does have a "built-in" way to log server side information to a log pane code editor window.新的代码编辑器确实具有将服务器端信息记录到日志窗格代码编辑器 window 的“内置”方式。 But, of course this assumes that you are running code from the code editor.但是,这当然假设您正在从代码编辑器运行代码。 This new feature avoids the need for changing browser tabs to see your logging output.此新功能无需更改浏览器选项卡即可查看您的日志记录 output。 I don't know of any way to log server side info to your browser console.我不知道有什么方法可以将服务器端信息记录到您的浏览器控制台。 You could save log info into an object, and then send it back to the client after the server code completed, and then log everything in the console.您可以将日志信息保存到 object 中,然后在服务器代码完成后将其发送回客户端,然后在控制台中记录所有内容。

The way that it might be possible to get logging information depends on how the code was originally triggered.获取日志信息的方式取决于最初触发代码的方式。

  • From code editor从代码编辑器
  • From a user using your app来自使用您应用的用户
  • From a Http Request to your Web App从 Http 请求到您的 Web 应用程序

Logging in Apps Script works differently depending upon:登录 Apps 脚本的工作方式不同,具体取决于:

  • run time version being used - V8 or DEPRECATED_ES5 - Set in appsscript.json file or through the "Run" menu in the Legacy editor, Set in "Settings" in new IDE - New Apps Script projects default to V8 so chances are your project is using V8.正在使用的运行时版本 - V8 或 DEPRECATED_ES5 - 在 appsscript.json 文件中或通过旧版编辑器中的“运行”菜单设置,在新 IDE 的“设置”中设置 - 新应用程序脚本项目默认为 V8,因此您的项目可能是使用 V8。
  • Is your Apps Script project associated with a Google Cloud Platform (GCP) default or standard project您的 Apps 脚本项目是与 Google Cloud Platform (GCP) 默认项目还是标准项目相关联
  • Is exception logging set to "exceptionLogging": "STACKDRIVER" - Set in appsscript.json file - Default is always to include it - Probably already correct unless you deleted it.异常日志是否设置为“exceptionLogging”:“STACKDRIVER” - 在 appsscript.json 文件中设置 - 默认始终包含它 - 除非您删除它,否则可能已经正确。
  • Using either Logger.log or console.log使用Logger.logconsole.log
  • Using console.log in the server ".gs" file or client side ".html" file.在服务器“.gs”文件或客户端“.html”文件中使用console.log console.log can be used in both server and client side code but the log print out goes to different places. console.log可以在服务器端和客户端代码中使用,但日志打印输出到不同的地方。 You can't see logs in the browser dev tools from a console.log statement in your server code.您无法通过服务器代码中的console.log语句查看浏览器开发工具中的日志。 If you use console.log in server side.gs files, and the Apps Script project is not associated with a standard GCP project, then the log only gets sent to your "Executions."如果您在服务器端.gs 文件中使用console.log ,并且 Apps 脚本项目与标准 GCP 项目关联,则日志只会发送到您的“执行”。 I believe that the only way that you can get your logs sent to Stackdriver is by using a standard GCP project.我相信将日志发送到 Stackdriver 的唯一方法是使用标准 GCP 项目。 The problem with that, is that you only have so many GCP projects that you can use without requesting an increase in your quota.这样做的问题是,您只有这么多 GCP 项目可以使用,而无需请求增加配额。

Plus there may be issues (bugs) depending upon how you have logging set up and other factors.另外,根据您设置日志记录的方式和其他因素,可能会出现问题(错误)。

For example: https://issuetracker.google.com/issues/134374008例如: https://issuetracker.google.com/issues/134374008

As lots of people specified, you can use console.log for this.正如很多人指定的那样,您可以为此使用console.log

However, I also work with webhooks from time to time.但是,我有时也会使用 webhook。 And I find it much more comfortable to debug directly into google spreadsheets, using code like this而且我发现直接调试到谷歌电子表格更舒服,使用这样的代码

function doPost(e) {
  log('# doPost', JSON.stringify(e));
  try {
    // Some webhook-processing logic here
    if(e.parameter.action == 'test-error') {
      item.something = nothing;
    }
    if(e.parameter.action == 'test-log') {
      log('# custom log', 'Some data');
    }
  } catch(error) {
    log('# error', JSON.stringify([error, error.stack]));
  }
}

function log(event, message){
  SpreadsheetApp.getActive().getSheetByName('Log').appendRow([new Date(), event, message])
}

Example spreadsheet: https://docs.google.com/spreadsheets/d/144i9pxDIB_C8ZRZ-6vd9DiPIw8Rt85hTtVoqk7dtHQg/edit?usp=sharing电子表格示例: https://docs.google.com/spreadsheets/d/144i9pxDIB_C8ZRZ-6vd9DiPIw8Rt85hTtVoqk7dtHQg/edit?usp=sharing

You can trigger logging by something like this您可以通过这样的方式触发日志记录

curl -X POST https://script.google.com/macros/s/AKfycby3EoaQ8DOt8H_Mc4vjH6JZkhsaAwYHk_sa9HE5Be3qVo0Ym0b2/exec?action=test-error

or或者

curl -X POST https://script.google.com/macros/s/AKfycby3EoaQ8DOt8H_Mc4vjH6JZkhsaAwYHk_sa9HE5Be3qVo0Ym0b2/exec?action=test-log

You can use same log for your custom logging of some intermediate variable during webhook resolution.您可以在 webhook 解析期间使用相同的日志来自定义一些中间变量的日志记录。

The reason I prefer this over standard stackdriver logging is that google sheets are more explicit and easier to manage.我更喜欢标准堆栈驱动程序日志记录的原因是谷歌表格更明确且更易于管理。

You can use console.log() to see things within "My Executions".您可以使用 console.log() 查看“我的执行”中的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM