Skip to content

Instantly share code, notes, and snippets.

@hamed-farag
Created August 25, 2018 22:06
Show Gist options
  • Save hamed-farag/e4470b1b8e52cb5da929a833f0d21ab1 to your computer and use it in GitHub Desktop.
Save hamed-farag/e4470b1b8e52cb5da929a833f0d21ab1 to your computer and use it in GitHub Desktop.
import React from "react";
export default WrapperComponent => {
const _logType = {
warn: "warn",
error: "error",
info: "info"
};
let _logging = {
info: function(logTypeName, filePath, description) {
// Console.info, Calling API to log it, winston-client, loglevel library or your custom code
},
warn: function(logTypeName, filePath, description) {
// Console.warn, Calling API to log it, winston-client, loglevel library or your custom code
},
error: function(logTypeName, filePath, description) {
// Console.error, Calling API to log it, winston-client, loglevel library or your custom code
}
};
let logging = function(logType = "info", logTypeName, filePath, description) {
switch (logType.toLowerCase()) {
case _logType.info:
_logging.info(logTypeName, filePath, description);
break;
case _logType.warn:
_logging.warn(logTypeName, filePath, description);
break;
case _logType.error:
_logging.error(logTypeName, filePath, description);
break;
default:
break;
}
};
return function(props) {
return (
<WrapperComponent
{...props}
logging={{ log: logging, type: { ..._logType } }}
/>
);
};
};
@hamed-farag
Copy link
Author

How to use it?

import React from "react";
import withLogging from "../HOC/loggingHOC";

class Home extends React.PureComponent {
render() {
const { logging: {log, type} } = this.props;

log(type.warn, 'TypeWarn', 'app/components/Usercard.jsx', 'This Warning to be Handled bla bla bla')
return <span>Hello World!</span>;
}
}

export default withLogging(Home);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment