• Upswift

Best Practice Guide - Send and receive device logs

This guide goes over the best practice to send and receive device logs as well as view them at Upswift platform. The best practice guidelines are used Upswift General API to send logs from connected devices to Upswift platform in the quickest way.

Send device logs approach

Upswift provides an easy-to-use way to send device logs from any type and view/download them from Upswift platform. These logs may help you to have a better understanding of the product behavior in the long-run, also, can be useful for debugging issues and investigation.


As a best practice, we would like to send daily/weekly logs of our device application, this can be done by using a separate script that will run by service manager/crontab, or inside the application code itself. In this best practice guide, we will create a weekly logs push script that runs once a week from each device.

Guidelines

Those guidelines require the device to be connected to Upswift platform. For more information about the Logs feature, read Upswift docs.


1. Create a python script (is it possible to work with any other programing language with HTTP library).

import json
import requests

UPSWIFT_SETTINGS_FILE = '/etc/upswift/service/settings.json'

#Change file name and file path according to your app log.
FILE_NAME = "log.txt"
FILE_PATH = "/home/myapp/log.txt"

#Pull Upswift tokens
with open(UPSWIFT_SETTINGS_FILE) as json_file:
    data = json.load(json_file)

#Upswift tokens
user_token = data['user_token']
device_token = data['device_token']

json_content = {'device_token': device_token,
                'user_token': user_token}


json_payload = (None, json.dumps(json_content), 'application/json')
file_payload = (FILE_NAME, open(FILE_PATH, 'rb'), 'application/octet-stream')
final_payload = {'json': json_payload,'file': file_payload}

call_request = requests.post("https://api.upswift.io/v1/send_log_file", files=final_payload)
call_response = json.loads(call_request.text)

if call_request.status_code != 200:
    if call_request.status_code == 429:
        error = "API limit reached"
    else:
        error = call_response["error_message"]
    print(error)
else:
    response_message = call_response["message"]


This script pulls Upswift account tokens, take the path of the log file and send it to Upswift servers. Make sure to change the values of FILE_NAME & FILE_PATH as needed.


2. To run our script each day or week, we will use crontab software and schedule when to call the python script.


Here is a ready-to-use command you need to run on the device terminal to creates a cron job:

echo "* * 1 * * python /home/app/send_logs.py" >> /etc/crontab

Make sure to change the path (in red) which indicates the place you store the python script.

From now on, your device will run the python script once a day.


3. Under the 'Devices' category, each device has a Log section where you will be able to view and download the received device logs.

Industries

Platform

Company

imgFlasher

Upswift©2020

Let's Talk.

  • Upswift Twitter
  • Facebook