---
# System prepended metadata

title: Invoke and Log Feature
tags: [' feature', cli]

---

---
tags: cli, feature
author: Oliver.W
---

[![hackmd-github-sync-badge](https://hackmd.io/f3yQPEuMTWuPAiryjN_VLw/badge)](https://hackmd.io/f3yQPEuMTWuPAiryjN_VLw)

# Invoke and Log Feature

## User Story

As a serverless developer  
I like to invoke the function on the cloud  
So that i can easily know the status and issues.  

## Logic Path

### Use Invoke

```sh
# Invoke the single SCF component function exported in serverless.yml
# get the SCF instance name from serverless.yml
$ serverless invoke

# Invoke the function of multi function components with a specific name.
# get the SCF instance name from serverless.yml
$ serverless invoke --function {name}

# Specific the stage and region of the invoke function 
# specific the name of the stage and region if SCF component allow different stage/region info. (should work for custom name `my-scf-${stage}`)
$ serverless invoke --function {name} --stage {stageName} --region {regionName}

# Invoke with data 
$ serverless invoke --function {name} --data {dataString}

# Invoke with path
$ serverless invoke --function {name} --path {dataString}

```

1. In SCF Component or Multipel SCF component folder
    * show the invoke in help command.
2. Generate the SCF instance info
    * SCF instance name:
        * Fullly Customized: Not support region and stage.
        * Part Customized(my-scf-${stage}): support stage.
        * Default(${app}-${stage}-${region}): support stage and region. 
3. Function is not required
    * use the default export function from SCF components.
    * need to specify for multip function component.



## Command Design

```sh
serverless invoke 调用云端函数
    
  -- `function` 或 `-f` 调用的多函数组件的函数名称(单函数组件无需指定)
  -- `stage` 或 `-s` 调用的函数的环境名称，默认使用配置环境
  -- `region` 或 `-r` 调用的函数的地区名称，默认使用配置地区
  -- `data` 或 `-d` 调用函数的事件(event)参数的序列化数据(String)
  -- `path` 或 `-p` 调用函数的事件(event)参数的 json 文件路径
```

## Usage:

### invoke function


```sh
--- 在非单函数或多函数组件调用invoke
Serverless: (黄色)Inovke 命令仅能在 SCF 组件目录中调用(/黄色)

--- 在未部署单函数或多函数组件调用invoke
--- 找不到指定实例
--- code: 1001
Serverless: (黄色)无法找到指定SCF实例，请检查SCF实例名称和 Stage / Region 信息或重新部署后调用。(/黄色)

--- ~在多函数组件中没有指定函数名称
Serverless: (黄色)当前应用包含多个函数，请指明要调用函数的具体名称(/黄色)

--- ~传递无法替代的Stage或Region信息
  -- 如： mySCF-{stage} 无法指定region
  -- 如： mySCF-{region} 无法指定stage
Serverless: (黄色)当前应用自定义SCF实例名称无法找到指定 Stage / Region 信息，请检查后重试(/黄色)

--- 同时传递了 data 和 path
Serverless: (黄色)不能同时指定data与path，请检查后重试(/黄色)

--- 传递了无效的event参数
Serverless: (黄色)传入的 data 不是序列化的 JSON，请检查后重试(/黄色)

--- 传递了无效的path
Serverless: (黄色)找不到指定的路径文件，请检查后重试(/黄色)

--- 项目配置为 web函数时
Serverless: (黄色)暂不支持WEB函数调用,请使用其他工具通过API网关地址直接调用.(/黄色)

--- 调用成功
serverless ⚡components
Action: "invoke" - Stage: "dev" - Region: "ap-guangzhou" - App: "my-scf" - ~Function: "my_handler"

... 输出结果

```

# Log Story

As a serverless developer  
I like to check the log on cloud  
So that i can check the log qeasily.  

## Logic Path

### Use Logs

```sh
# Check logs of the current service/function
$ serverless logs

$ serverless logs --stage {stageName} --region {regionName}

$ serverless logs -f functionName

$ serverless logs --startTime

$ serverless logs --tail

$ serverless logs -t -i 4000

```

1. In SCF Component or Multipel SCF component folder
    * show the invoke in help command.
2. Generate the SCF instance info
    * SCF instance name:
        * Fullly Customized: Not support region and stage.
        * Part Customized(my-scf-${stage}): support stage.
        * Default(${app}-${stage}-${region}): support stage and region. 
3. Function is not required
    * use the default export function from SCF components.
    * need to specify for multip function component.



## Command Design

```sh
serverless logs 调用云端函数

  -- `--function` 或 `-f` 查看指定函数的日志，默认使用配置的函数
  -- `--stage` 或 `-s` 查看日志的环境名称，默认使用配置环境
  -- `--region` 或 `-r` 查看日志地区名称，默认使用配置地区
  -- `--startTime` 查看日志的开始时间，如：3d, 20130208T080910，默认10m
  -- `--tail` 或 `-t` 查看最新日志
  -- `--interval` 或 `-i` 最新日志的刷新时间 默认：1000ms
```

## Usage:


```sh
--- 传递无法替代的Stage
  -- 如： mySCF-{stage} 无法指定stage
Serverless: (黄色)当前应用自定义SCF实例名称无法找到指定 Stage / Region 信息，请检查后重试(/黄色)

--- 在未部署组件调用logs
--- 找不到指定实例
--- 1001
Serverless: (黄色)无法找到指定SCF实例，请检查SCF实例名称和 Stage / Region 信息或重新部署后调用(/黄色)

--- 传递了时间格式不正确。
Serverless: (黄色)指定时间格式不正确，请检查后重试(/黄色)

--- 日志结果为空的时候
serverless ⚡components
Action: "logs" - Stage: "dev" - App: "my-scf" - Name: "scf-app" - Region: "ap-guangzhou" 

(灰色)当前时间范围内没有可用的日志信息（/灰色）


--- 调用成功
serverless ⚡components
Action: "logs" - Stage: "dev" - App: "my-scf" - Name: "scf-app" - Region: "ap-guangzhou" 

... 输出结果
START RequestID: xxx
...
Report ...

```

## Implementatoin Design

### Logs

* Engine provide new api to return the updated logs.

> getLogs(startTime, endTime, limit, query, cursor)
  * logs: getLogs(start, end, 100, query)
  * tails: getlogs(null, null, 100, query, cursor)


#### Logs
Get the time specific logs result directly.

#### tails
Get updated tails log with cursor, and added to the console. 



