# smklog

**Purpose**

Generic way of logging that can be used across services and platforms to provide easily searchable data in our ELK cluster.
Most important field is 'request_id' allowing us to trace a requests journey through the architecture.

**Library**

Log library should support both python and node JS. 
Can be open sourced and published to public npm/pypi (via gitlab ci) for ease of use.

**Log Message Fields**

- request_id (optional): Original request ID, this should be added to swagger API as header

This is an optional header used to pass original request ID through flow (e.g. storefront api gateway / nginx -> storefront service -> nomad api gateway -> nomad fc)

Actual ID is generated by port of first call, normally api gateway

If present should be used as request_id in log message, if not present should use alternative e.g. api gateway request ID

- platform: e.g. youzan, or 'storefront_wechat' etc. for our own platform

- service: e.g. nomad_envoy_order or storefront_auth

- source: Who made the request if known, e.g. odoo, or storefront_user for storefront

- category: Type of event e.g. debug, error, info, database_query, kafka_message

- label: Label for event e.g. update_user_avatar, translate_error_message

- message: Readable message describing event e.g. "Translated error message for youzan service not responding"

- data: If message carries json data e.g. http request/response or data that can be JSON stringified.

- user: Identifier for user instigating log message e.g. storefront user ID, or app (api key) ID in case of nomad user

- store_id (optional): Store ID if known e.g. samarkand.youzan.foreveryoung or samarkand.storefront.mybrandstore


**Usage**

```python
from smklog import SmkLog

platform = "youzan"
service = "nomad_envoy_order"

logger = SmkLog(platform, service)

source = "odoo"
category = "info"
label = "get_all_orders"

message = "Translated error message for youzan service not responding"

data_json = {"key":"value","键":"值"}

user = "1234567"

store_id = "samarkand.youzan.foreveryoung"
request_id = "0EFD81C9-16BF-4FF6-8CF8-00FEB0619A47"


logger.log(source, category, label, message, data_json, user, store_id, request_id)
```

The output would be like

```txt
2019-01-16 12:04:45,067 - youzan_nomad_envoy_order - INFO - {"platform":"youzan","service":"nomad_envoy_order","source":"odoo","category":"info","label":"get_all_orders","message":"Translated error message for youzan service not responding","data":"{\"key\":\"value\",\"键\":\"值\"}","user":"1234567","store_id":"samarkand.youzan.foreveryoung","request_id":"0EFD81C9-16BF-4FF6-8CF8-00FEB0619A47"}
```