Skip to content

Instantly share code, notes, and snippets.

@neogeek
Last active August 15, 2020 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neogeek/46e7896edaf8a59089b19fb07577555f to your computer and use it in GitHub Desktop.
Save neogeek/46e7896edaf8a59089b19fb07577555f to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
namespace LumberLogs
{
public class Logger : MonoBehaviour
{
#pragma warning disable CS0649
[SerializeField]
private string url;
#pragma warning restore CS0649
private int failedConnections;
private const int maxFailedConnections = 10;
#if UNITY_EDITOR || DEVELOPMENT_BUILD
private void OnEnable()
{
Application.logMessageReceived += HandleLog;
}
private void OnDisable()
{
Application.logMessageReceived -= HandleLog;
}
#endif
private void HandleLog(string logString, string stackTrace, LogType type)
{
if (url == null || failedConnections >= maxFailedConnections)
{
return;
}
var loggingForm = new WWWForm();
loggingForm.AddField("Type", type.ToString());
loggingForm.AddField("Message", logString);
loggingForm.AddField("Stack_Trace", stackTrace);
loggingForm.AddField("Device_Model", SystemInfo.deviceModel);
StartCoroutine(SendDataToLumberLog(loggingForm));
}
private IEnumerator SendDataToLumberLog(WWWForm form)
{
using (var www = UnityWebRequest.Post(url, form))
{
yield return www.SendWebRequest();
if (!www.isNetworkError && !www.isHttpError)
{
yield break;
}
Debug.LogError(www.error);
failedConnections += 1;
}
}
}
}
fileFormatVersion: 2
guid: 54098c83cc90b45bb9d72178609b7d64
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
{
"name": "LumberLogs"
}
fileFormatVersion: 2
guid: f83e7ec76431346329b76b1aa2545535
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
{
"name": "com.scottdoxey.lumberlogs",
"displayName": "Lumber Logs",
"version": "1.0.0",
"unity": "2019.3",
"description": "A self-hosted log aggregation tool."
}
fileFormatVersion: 2
guid: b6675c8348c1c404783a4f1abae64ecf
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment