Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My try at LogBox Slack Appender #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/settings.xml
/.settings/
/.project
68 changes: 0 additions & 68 deletions SlackAppender.cfc

This file was deleted.

155 changes: 155 additions & 0 deletions slackappender/ModuleConfig.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/**
Module Directives as public properties
this.title = "Title of the module";
this.author = "Author of the module";
this.webURL = "Web URL for docs purposes";
this.description = "Module description";
this.version = "Module Version";
this.viewParentLookup = (true) [boolean] (Optional) // If true, checks for views in the parent first, then it the module.If false, then modules first, then parent.
this.layoutParentLookup = (true) [boolean] (Optional) // If true, checks for layouts in the parent first, then it the module.If false, then modules first, then parent.
this.entryPoint = "" (Optional) // If set, this is the default event (ex:forgebox:manager.index) or default route (/forgebox) the framework
will use to create an entry link to the module. Similar to a default event.
this.cfmapping = "The CF mapping to create";
this.modelNamespace = "The namespace to use for registered models, if blank it uses the name of the module."

structures to create for configuration
- parentSettings : struct (will append and override parent)
- settings : struct
- datasources : struct (will append and override parent)
- interceptorSettings : struct of the following keys ATM
- customInterceptionPoints : string list of custom interception points
- interceptors : array
- layoutSettings : struct (will allow to define a defaultLayout for the module)
- routes : array Allowed keys are same as the addRoute() method of the SES interceptor.
- wirebox : The wirebox DSL to load and use

Available objects in variable scope
- controller
- appMapping (application mapping)
- moduleMapping (include,cf path)
- modulePath (absolute path)
- log (A pre-configured logBox logger object for this object)
- binder (The wirebox configuration binder)
- wirebox (The wirebox injector)

Required Methods
- configure() : The method ColdBox calls to configure the module.

Optional Methods
- onLoad() : If found, it is fired once the module is fully loaded
- onUnload() : If found, it is fired once the module is unloaded

*/
component {

// Module Properties
this.title = "SlackAppender";
this.author = "Bogdan";
this.webURL = "";
this.description = "";
this.version = "0.1";
// If true, looks for views in the parent first, if not found, then in the module. Else vice-versa
this.viewParentLookup = true;
// If true, looks for layouts in the parent first, if not found, then in module. Else vice-versa
this.layoutParentLookup = true;
// Module Entry Point
this.entryPoint = "SlackAppender";
// Model Namespace
this.modelNamespace = "";
// CF Mapping
this.cfmapping = "";

function configure(){

// parent settings
parentSettings = {

};

// module settings - stored in modules.name.settings
settings = {

};

// Layout Settings
layoutSettings = {
defaultLayout = ""
};

// datasources
datasources = {

};

// SES Routes
routes = [
// Module Entry Point
{pattern="/", handler="home",action="index"},
// Convention Route
{pattern="/:handler/:action?"}
];

// Custom Declared Points
interceptorSettings = {
customInterceptionPoints = ""
};

// Custom Declared Interceptors
interceptors = [
];

// Binder Mappings
// binder.map("Alias").to("#moduleMapping#.model.MyService");
binder.map("SlackService@slackappender").to("#moduleMapping#.model.SlackService");
binder.map("SlackAttachment@slackappender").to("#moduleMapping#.model.SlackAttachment");

}

/**
* Fired when the module is registered and activated.
*/
function onLoad(){
loadAppenders();
}

/**
* Fired when the module is unregistered and unloaded
*/
function onUnload(){

}

// load LogBox appenders
private function loadAppenders(){
// Get config
var logBoxConfig = controller.getLogBox().getConfig();
var rootConfig = "";

rootConfig = logBoxConfig.getRoot();
logBoxConfig.appender(
name="SlackAppender",
class="#moduleMapping#.model.SlackAppender",
properties={
webhookURL = "https://ngn-power.slack.com/services/hooks/incoming-webhook?token=6AGstDGAzLeINGEx6i3Aofmu",
channel = "##deployments"
}
);


// Store back config
controller.getLogBox().configure( logBoxConfig );
}

public function development(){
logBox.getConfig().category(name="Slack", levelMin="FATAL", levelMax="ERROR",appenders="SlackAppender");
}

public function qa(){
logBox.getConfig().category(name="Slack", levelMin="FATAL", levelMax="INFO",appenders="SlackAppender");
}

public function prod(){
logBox.getConfig().category(name="Slack", levelMin="FATAL", levelMax="INFO",appenders="SlackAppender");
}

}
89 changes: 89 additions & 0 deletions slackappender/box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"name":"Slack-Appender",
"version":"0.5.0.0000",
"author":"Bogdan Pop-C.",
"location":"",
"directory":"",
"createPackageDirectory":true,
"packageDirectory":"",
"homepage":"",
"documentation":"",
"repository":{
"type":"",
"url":""
},
"bugs":"",
"slug":"mySlug",
"shortDescription":"",
"description":"",
"instructions":"",
"changelog":"",
"type":"",
"keywords":[

],
"private":false,
"engines":[
{
"type":"railo",
"version":">=4.1.x"
},
{
"type":"adobe",
"version":">=10.0.0"
}
],
"defaultEngine":"",
"defaultPort":0,
"projectURL":"",
"license":[
{
"type":"",
"url":""
}
],
"contributors":[

],
"dependencies":{

},
"devDependencies":{

},
"installPaths":{

},
"ignore":[

],
"testbox":{
"runner":[
{
"default":""
}
],
"labels":[

],
"reporter":"",
"reporterResults":"",
"bundles":[
""
],
"directory":{
"mapping":"",
"recurse":true
},
"watchers":[

],
"notify":{
"emails":[

],
"growl":"",
"url":""
}
}
}
46 changes: 46 additions & 0 deletions slackappender/model/SlackAppender.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
component extends="coldbox.system.logging.AbstractAppender" displayname="SlackAppender" output="false"{
public SlackAppender function init(required name, properties="#StructNew()#", layout="", levelMin="0", levelMax="4")
output="false"{

// Init supertype

super.init(argumentCollection=arguments);

// Verify properties
if( NOT propertyExists('webhookURL') ){
$throw(message="No webhookURL property defined",type="SlackAppender.InvalidProperty");
}
//defaults
if( NOT propertyExists('channel') ){
setProperty("channel",'');
}
if( NOT propertyExists('username') ){
setProperty("username",'');
}
variables.slackService = new SlackService(getProperty("webhookURL"));
return this;
}

public function logMessage(required any logEvent hint="The logging event") {
var loge = arguments.logEvent;
var payload = "";
var message = new SlackMessage(loge.getMessage(), getProperty("channel"));
var attachment = new SlackAttachment();
attachment.addField(title="Severity",value=logEvent.getSeverity(), short=true);
attachment.addField(title="Category",value=loge.getCategory(), short=true);
message.addAttachment(attachment);

try{
if( Len(loge.getExtraInfoAsString()) ){
var extraInfo = new SlackAttachment();
extraInfo.addField(title="Extra Info",value=loge.getExtraInfoAsString());
message.addAttachment(extraInfo);
}

variables.slackService.notify(message);

}catch(any e){
$log("ERROR","Error sending message to slack.com from appender #getName()#. #e.message# #e.detail# #e.stacktrace#");
}
}
}
25 changes: 25 additions & 0 deletions slackappender/model/SlackAttachment.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
component displayname="SlackAttachment" output="false" accessors="true"
{
property name="fallback" type="string" default="";
property name="color" type="string" default="";
property name="text" type="string" default="";
property name="pretext" type="string" default="";
property name="fields" type="array" ;

//Init
public SlackAttachment function init(string fallback = "", string color = "##D00000", array fields = [], string text = "", string pretext = ""){
variables.fallback = arguments.fallback;
variables.color = arguments.color;
variables.text = arguments.text;
variables.pretex = arguments.pretext;
variables.fields = arguments.fields;
return this;
}

/*
addField
*/
public function addField(required string title, required string value, string short = false){
ArrayAppend(variables.fields, {"title" = arguments.title, "value" = arguments.value, "short" = arguments.short});
}
}
Loading